简体   繁体   中英

Server Side validation not working - ASP.NET

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();
        }        
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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