简体   繁体   English

RegularExpressionValidator asp.net

[英]RegularExpressionValidator asp.net

I am trying to make sure that user only can input az and 0-9 characters. 我试图确保用户只能输入az0-9字符。 To do this I have used the RegularExpressionValidator class: 为此,我使用了RegularExpressionValidator类:

<asp:TextBox ID="input" Text="search" runat="server" OnTextChanged="searchFunc"></asp:TextBox>
<asp:RegularExpressionValidator ID="regExp" runat="server"      
                                    ErrorMessage="only a-z or 0-9 allowed" 
                                    ControlToValidate="input"         
                                    ValidationExpression="^[a-z0-9]+$" />

Though in the code behind I am trying to check if the user entered valid input by using 虽然在后面的代码中,我试图检查用户是否通过使用输入了有效输入

regExp.IsValid

But this method returns True even if user inputs !&%() I can't understand what I've done wrong. 但是,即使用户输入了!&%()我也无法理解自己做错了什么,该方法仍返回True。 Is it my regular expression that is wrong? 我的正则表达式错了吗?

Call regExp.IsValid only after the validation has been performed otherwise the default value is set to true. 仅在执行验证后才调用regExp.IsValid,否则默认值设置为true。

Try calling regExp.Validate() before checking the IsValid property. 在检查IsValid属性之前,请尝试调用regExp.Validate()。

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

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