asp.netregexvalidationreg-expressionvalidator

RegularExpressionValidator asp.net


I am trying to make sure that user only can input a-z and 0-9 characters. To do this I have used the RegularExpressionValidator class:

<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. Is it my regular expression that is wrong?


Solution

  • Call regExp.IsValid only after the validation has been performed otherwise the default value is set to true.

    Try calling regExp.Validate() before checking the IsValid property.