Search code examples
c#asp.netvalidationpostbackrequiredfieldvalidator

ASP Validators not working?


I feel embarrassed to be posting this as I'm pretty sure I'm missing something really simple, and this certainly isn't the first time I've used asp validators. I have the following validator controls:

<asp:RequiredFieldValidator runat="server" ControlToValidate="txtTitle" EnableClientScript="false" ErrorMessage="*" ValidationGroup="groupUpdateAL" />

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAbbr" EnableClientScript="false" ErrorMessage="*" ValidationGroup="groupUpdateAL" />

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlColour" EnableClientScript="false" ValidationGroup="groupUpdateAL" ErrorMessage="*" />

<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="groupUpdateAL" CausesValidation="true" /><br />

For some reason, if leaving the controls empty I am given the following error:

Length cannot be less than zero.
Parameter name: length

It seems the validation controls aren't catching an invalid input.

What I've Tried:

I suspected it may have been client validation intercepting the postback, which is why you can see EnableClientScript is set to false now. Didn't work.


Solution

  • Try testing page validity in the onclick event of your btnSave :

    if(Page.IsValid)
    {
        //Do your stuff
    }
    else
    {
        Response.Write("error");
    }