繁体   English   中英

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

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

我有一个asp:文本框从用户那里取一个容器号并验证它,我怎样才能确保输入的字符串正好是11个字符长

  1. 前4个字符是普通英文字符
  2. 第四个字符必须是以下{U或J或Z}之一
  3. 其余(9个字符)是数字。

示例>“CRLU123456789”

我建议您使用RegularExpressionValidator 它们很容易标记:

<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" />

现在,使用EnabledClientScript="true"它将实际使用JavaScript验证控件,如果它已启用,但是因为您仍然不能依赖它,您也需要验证服务器端。 那么,你要做的就是在按钮点击中,你想要进行验证,第一个代码块应该是:

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

当您回发时,将显示错误消息。

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

暂无
暂无

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

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