简体   繁体   English

文本框输入控件(字符数,字符类型)

[英]Textbox input control (number of characters, type of characters)

I have an asp:textbox to take a container number from the user and validate it, how can I make sure that the entered string is exactly 11 characters long of which 我有一个asp:文本框从用户那里取一个容器号并验证它,我怎样才能确保输入的字符串正好是11个字符长

  1. The first 4 characters are ordinary English characters 前4个字符是普通英文字符
  2. The 4th character must be one of the following {U or J or Z} 第四个字符必须是以下{U或J或Z}之一
  3. The remaining (9 characters) are digits. 其余(9个字符)是数字。

Example > "CRLU123456789" 示例>“CRLU123456789”

I would recommend that you use a RegularExpressionValidator . 我建议您使用RegularExpressionValidator They are very easy to markup: 它们很容易标记:

<asp:RegularExpressionValidator
    ControlToValidate="NameOfTextBox"  <!-- the input text box name -->
    ValidationExpression="[a-zA-Z]{4}[UJZ]\d{9}"
    ErrorMessage="Please enter a valid container number."
    Display="Dynamic"  <!-- this means it's display: none when no error -->
    EnabledClientScript="true"  <!-- this will perform JavaScript validation -->
    runat="server" />

Now, with the EnabledClientScript="true" it will actually validate the control with JavaScript, if it's enabled, but because you still can't rely on that you need to validate server-side too. 现在,使用EnabledClientScript="true"它将实际使用JavaScript验证控件,如果它已启用,但是因为您仍然不能依赖它,您也需要验证服务器端。 So, what you'll do is in the button click, where you want validation to occur, the first block of code should be: 那么,你要做的就是在按钮点击中,你想要进行验证,第一个代码块应该是:

this.Validate();    // validates all Validators on the page
if (!this.IsValid) { return; }

and when you post back the error message will be displayed. 当您回发时,将显示错误消息。

对此ValidationExpression使用RegularExpressionValidator控件: [a-zA-Z]{4}[ujzUJZ][0-9]{9}

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

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