简体   繁体   中英

limiting the text box to have only particular limit of numbers and characters

I have a text box with label name Employee Id and a text box to provide employee id now to this text box i should set a validation to have a limit up to 5 numbers or characters if the user tries to enter more than 5 characters or numbers it should display an error message your maximum limit is only 5.

Employee ID*

here i have no button to the text box hence while entering only if the limit exceeds 5 it should display the error message before going to next text box.can you please help me out.

Below code will help you. Maximum and Minimum character length validation for 5 character.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,5}$" runat="server" ErrorMessage="Your maximum limit is only 5"></asp:RegularExpressionValidator>


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{5,}$" runat="server" ErrorMessage="Minimum required limit is 5"></asp:RegularExpressionValidator>

In ASP.NET you will have validation controls, for this case you can use Regular Expression controller

<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,5}$" runat="server" ErrorMessage="Maximum 5 characters allowed."></asp:RegularExpressionValidator>

If you are using HTML5 you can set a pattern also like below that also will show validation

<input type="text" maxlength="5" pattern=".{5,5}" class="text-center" autocomplete="off" id="yearofManufacture" required  placeholder="Year of Manufacture">

In the second approach you don't need to add any plugins also

try this it will work

 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate ="TextBox2" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,5}$" runat="server" ErrorMessage="Maximum 5 characters allowed."></asp:RegularExpressionValidator>

You can use JQuery Validation :

$("#myinput").rules( "add", {
  required: true,
  maxlength: 5,
  messages: {
    required: "Required input",
    minlength: jQuery.validator.format("Please, at most {0} characters are allowed")
  }
});

You can include it in your project via Nuget or Visual Studio Nuget Manger

Follow this step by step Tutorial : JQuery Validation Tutorial

You can set the textBox.MaxLength=5; so the user can't actually write more than 5 characters, why would you need an error message? This should be enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM