[英]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个字符长
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.