0

Client side validation works fine. I disabled the client side to see if it also works good server side, but fails. The compiler reaches 'SaveData' even if the input text is invalid. There are no update panels. How should I solve this.

ASPX:

<asp:TextBox ID="txtDept" runat="server" pattern="[a-z A-Z]*"></asp:TextBox>

<asp:RegularExpressionValidator 
    ID="revDept" 
    runat="server" 
    ValidationExpression="^[a-zA-Z\s]{1,50}$" 
    ControlToValidate="txtDept" 
    Display="Dynamic" 
    ErrorMessage="Only alphabets and spaces are allowed." 
    EnableClientScript="false">
</asp:RegularExpressionValidator>

C#:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
          SaveData();
        }        
    }
6
  • 4
    Explicitly call Page.Validate() on the server side. Commented Sep 29, 2014 at 6:32
  • 2
    Refer: stackoverflow.com/questions/13762467/… Commented Sep 29, 2014 at 6:34
  • 2
    Also, try removing the pattern from the textbox. You might run into a pattern conflict with both (and they are not equivalent) Commented Sep 29, 2014 at 6:36
  • This is for a WAP site & so I need to put that. Any other alternatives Commented Sep 29, 2014 at 6:44
  • @Sain. Thank you that worked. I remember, it worked for other forms even without explicitly calling validate(). Any reason. Or I just use it by trial?? Commented Sep 29, 2014 at 6:46

1 Answer 1

1

You need to either have "CausesValidation" enabled on the submit button (we can't check in your code if it is so), or to call explicitly "Page.Validate()" before to test the IsValid property.

Please also take a look at How does Page.IsValid work? , it could be helful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.