Search code examples
c#asp.netvalidationvalidationsummary

Validation Summary Not Displaying Errors In Proper Place


I have two groups in my login page, login and createUser. I have two validation summaries, loginSum and createSum. I have Required Field validation on every control. If I click the Login or Create button without the required field having values, the errors will appear below the field, instead of in the summary.

Login code:

<div id="login" class="settings">
asp:Label ID="lblInvalid" runat="server" Visible="false" Display="Static">
        YOUR EMAIL OR PASSWORD WERE NOT FOUND. PLEASE REVIEW YOUR INFORMATION AND TRY AGAIN.<br /></asp:Label>
    <asp:Label ID="Label1" runat="server" Text="Email:" Width="100px" />
    <asp:TextBox ID="user" runat="server" /><br />
    <%-- validation for user login email --%>
    <asp:RegularExpressionValidator ID="validEmail"
        ControlToValidate="user"
        ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
        Display="Static"
        ValidationGroup="login"
        ErrorMessage="Email"
        SetFocusOnError="true"
        runat="server" 
        Text="*" /><br />
    <asp:Label ID="Label2" runat="server" Text="Password:" Width="100px" />
    <asp:TextBox ID="password" TextMode="password" runat="server" /><br />
    <%-- validation for user login password --%>
    <asp:RequiredFieldValidator ID="passReq"
        runat="server"
        ControlToValidate="password"
        ValidationGroup="login"
        ErrorMessage="Password"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Button ID="submit" runat="server" Text="Login" OnClick="login_Click"
        ValidationGroup="login" CausesValidation="true" /><br />
    <asp:ValidationSummary ID="loginSum"
        DisplayMode="BulletList"
        EnableClientScript="true"
        ShowSummary="true"
        HeaderText="You must enter a valid value for the following fields:"
        runat="server" />
</div>

Create code:

<div id="create_account" class="settings">
    <asp:Label ID="Label10" runat="server" Text="Name:" Width="100px" />
    <asp:TextBox ID="txtName" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="nameReq"
        runat="server"
        ControlToValidate="txtName"
        ValidationGroup="createUser"
        ErrorMessage="You must put in a name!"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="Label3" runat="server" Text="Email Address:" Width="100px" />
    <asp:TextBox ID="txtNewUser" runat="server" /><br />
    <%-- validation for new email --%>
    <asp:RequiredFieldValidator ID="newUserReq"
        runat="server"
        ControlToValidate="txtNewUser"
        ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
        ValidationGroup="createUser"
        ErrorMessage="Email address"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="Label4" runat="server" Text="Password:" Width="100px" />
    <asp:TextBox ID="txtNewPass" TextMode="password" runat="server" /><br />
    <%-- validation for new password --%>
    <asp:RequiredFieldValidator ID="newPassReq"
        runat="server"
        ControlToValidate="txtNewPass"
        ValidationGroup="createUser"
        ErrorMessage="Password"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="Label5" runat="server" Text="Confirm Password:" Width="100px" />
    <asp:TextBox ID="txtConfirmPass" TextMode="password" runat="server" /><br />
    <%-- validation for confirm password --%>
    <asp:RequiredFieldValidator ID="confirmPassReq"
        runat="server"
        ControlToValidate="txtConfirmPass"
        ValidationGroup="createUser"
        ErrorMessage="Confirm password"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:CompareValidator ID="ComparePass" runat="server"
        ControlToValidate="txtConfirmPass"
        CssClass="ValidationError"
        ControlToCompare="txtNewPass"
        ErrorMessage="The passwords you entered must match" /><br />
    <asp:Label ID="label" Text="Address:" runat="server" Width="100px" />
    <asp:TextBox ID="txtAddress" TextMode="multiline" Columns="22" Rows="3" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="addressReq"
        runat="server"
        ControlToValidate="txtAddress"
        ValidationGroup="createUser"
        ErrorMessage="Address"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label6" Text="City:" runat="server" Width="100px" />
    <asp:TextBox ID="txtCity" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="cityReq"
        runat="server"
        ControlToValidate="txtCity"
        ValidationGroup="createUser"
        ErrorMessage="City"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label7" Text="State:" runat="server" Width="100px" />
    <asp:TextBox ID="txtState" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="stateReq"
        runat="server"
        ControlToValidate="txtState"
        ValidationGroup="createUser"
        ErrorMessage="State (abbreviation)"
        ValidationExpression="[a-zA-Z]{2}"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label8" Text="Zip Code:" runat="server" Width="100px" />
    <asp:TextBox ID="txtZip" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="zipReq"
        runat="server"
        ControlToValidate="txtZip"
        ValidationGroup="createUser"
        ErrorMessage="Zip code"
        ValidationExpression="[a-zA-Z]{9}"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label9" Text="Phone Number:" runat="server" Width="100px" />
    <asp:TextBox ID="txtPhone" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="phoneReq"
        runat="server"
        ControlToValidate="txtPhone"
        ValidationGroup="createUser"
        ErrorMessage="Phone number"
        ValidationExpression="[a-zA-Z]{15}"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Button ID="create" runat="server"
        Text="Create Account" OnClick="create_Click"
        ValidationGroup="createUser" CausesValidation="true" /><br />
    <br />
    <asp:ValidationSummary ID="createSum"
        DisplayMode="BulletList"
        ShowSummary="true"
        EnableClientScript="true"
        HeaderText="You must enter a valid value for the following fields:"
        runat="server" />
</div>

Can anyone help me or direct me to an effective tutorial? All I get right now is the "*" below everything, and if I take away the Text="*" then it just displays the ErrorMessage="_____" below every associated control, instead of in a Validation Summary like it's supposed to.


Solution

  • You will need to add validation group properties to all validators and both summaries, as well as the buttons (each button will only validate one group) ie

    ValidationGroup="logingroup"
    ValidationGroup="creategroup"
    

    Please see(^)