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();
        }        
    }
Brett Weber
  • 1,839
  • 18
  • 22
sukesh
  • 2,379
  • 12
  • 56
  • 111
  • 4
    Explicitly call Page.Validate() on the server side. – Sain Pradeep Sep 29 '14 at 06:32
  • 2
    Refer: http://stackoverflow.com/questions/13762467/how-does-page-isvalid-work?answertab=votes#tab-top – G J Sep 29 '14 at 06:34
  • 2
    Also, try removing the pattern from the textbox. You might run into a pattern conflict with both (and they are not equivalent) – Andre Calil Sep 29 '14 at 06:36
  • This is for a WAP site & so I need to put that. Any other alternatives – sukesh Sep 29 '14 at 06: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?? – sukesh Sep 29 '14 at 06:46
  • please check your submit button "CausesValidation" property. If it is not set true then you need to call explicitly on page behind. For more details check this http://msdn.microsoft.com/en-us/library/0ke7bxeh%28v=vs.110%29.aspx – Sain Pradeep Sep 29 '14 at 07:04

1 Answers1

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.

Community
  • 1
  • 1
AFract
  • 8,868
  • 6
  • 48
  • 70