简体   繁体   English

限制文本框只有特定的数字和字符限制

[英]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的文本框和一个文本框,用于向此文本框提供员工ID我应该设置验证,如果用户尝试输入超过5个字符或数字,则限制最多5个数字或字符它应显示一条错误消息,您的最大限制仅为5。

Employee ID* 员工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. 这里我没有按钮到文本框,因此只有当限制超过5时才进入它应该显示错误信息,然后转到下一个文本框。你可以帮帮我。

Below code will help you. 下面的代码将帮助您。 Maximum and Minimum character length validation for 5 character. 5个字符的最大和最小字符长度验证。

<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.NET中,您将拥有验证控件,在这种情况下,您可以使用正则表达式控制器

<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 如果您使用的是HTML5,您可以设置如下所示的模式,同时也会显示验证

<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 : 您可以使用JQuery验证

$("#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 您可以通过NugetVisual Studio Nuget Manger将其包含在您的项目中

Follow this step by step Tutorial : JQuery Validation Tutorial 按照此步骤教程JQuery验证教程

You can set the textBox.MaxLength=5; 你可以设置textBox.MaxLength=5; so the user can't actually write more than 5 characters, why would you need an error message? 所以用户实际上不能写超过5个字符,为什么你需要一个错误信息? This should be enough. 这应该足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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