Search code examples
asp.netrequiredfieldvalidator

Asp.net Required Field Validator Issue


I am using asp.net validations and jquery validation for checking user input.I am using number of required field validators and custom validators in my form , i am facing a strange problem , when a field with required validator is empty , the error message is displayed on validation summary. this is all right.When the same field is left empty and wrong inputs are entered on other fields with custom validators ,, the validation summary only displays the error message of required field validator.The other error messages are just not displayed.Can anybody point me out where am i going wrong. Thanks


Solution

  • Here is a code sample that uses RequiredFieldValidator together with CustomValidator. It validates both fields, as well as when one of them fails, and displays the error messages in the ValidationSummary.

    The CustomValidator has OnServerValidate function in the code behind.

    The .aspx markup:

                <form id="form1" runat="server">
                    <div>
                        <label>Name:</label>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" EnableClientScript="false"
                        ControlToValidate="TextBox1" ErrorMessage="The 'Name' field cannot be empty!" Text="*" ForeColor="Red" 
                        Display="Dynamic"></asp:RequiredFieldValidator>
    
                        <br /><br />
    
                        <label>Number:</label>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                        <asp:CustomValidator ID="CustomValidator1" runat="server" ValidateEmptyText="true"
                        ControlToValidate="TextBox2" ErrorMessage="The 'Number' field must be exactly 5 digits!" Text="*" ForeColor="Red" 
                        Display="Dynamic" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
    
                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please check the following fields:" ForeColor="Red" DisplayMode="BulletList" />
    
                        <br />
    
                        <asp:Button ID="Button1" runat="server" Text="Submit" />
                    </div>
                </form>
    

    and the validation function in .aspx.cs file:

                protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
                {
                    if (args.Value.Length < 5 || args.Value.Length > 5)
                    {
                        args.IsValid = false;
                    }
                    else
                    {
                        args.IsValid = true;
                    }
                }
    

    In order to be able to display both error messages, and not only the RequiredFieldValidator's, its property 'EnableClientScript' should be set to false.

    Hopefully, this will get you going and solve your problem.