[英]Textbox input control (number of characters, type of characters)
我有一个asp:文本框从用户那里取一个容器号并验证它,我怎样才能确保输入的字符串正好是11个字符长
示例>“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.