Search code examples
c#asp.netvalidationrequiredfieldvalidator

Validation on a button click using RequiredFieldValidator


In the past, on button click events, I've validated without using RequiredFieldValidators. However, I thought I'd learn about them and implement them.

My old approach:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (txtSubject.Text.Equals("") || txtEmail.Text.Equals("") || txtComments.Text.Equals(""))
    {
        lblMessage.Text = "Please check all fields have been entered.";
    }
    //else if ...further validation statements e.g. check lengths
}

However, using RequiredFieldValidators with the same example, am I correct in saying that I don't have to check again if (txtSubject.Text.Equals("") || txtEmail.Text.Equals("") || txtComments.Text.Equals("")) like below or is it good practice to do so?

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        //...further validation statements e.g. check lengths
        try
        {
            SendMail();
        }
        catch (Exception)
        {
        }
    }
}

If I should still include the line, it should go at the beginning of the if (Page.IsValid), right?

HTML code:

<p>Contact Form</p>
<p>
    Your name:
    <asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="*"
        ControlToValidate="txtName" ValidationGroup="save" /><br />
    <asp:TextBox ID="txtName" runat="server" Width="250px" /><br />
    Your email address:
    <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="*"
        ControlToValidate="txtEmail" ValidationGroup="save" /><br />
    <asp:TextBox ID="txtEmail" runat="server" Width="250px" />
    <asp:RegularExpressionValidator runat="server" ID="rfvEmail2"
        SetFocusOnError="true" Text="Example: email@gmail.com" ControlToValidate="txtEmail"
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
        ValidationGroup="save" /><br />
    Subject:
    <asp:RequiredFieldValidator ID="rfvSubject" runat="server" ErrorMessage="*"
        ControlToValidate="txtSubject" ValidationGroup="save" /><br />
    <asp:TextBox ID="txtSubject" runat="server" Width="400px" /><br />
    Comments:
    <asp:RequiredFieldValidator ID="rfvComments" runat="server" ErrorMessage="*"
        ControlToValidate="txtComments" ValidationGroup="save" /><br />
    <asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" />
</p>
<p>
    <asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" ValidationGroup="save" />
</p>
<p>
    <asp:Label ID="lblMessage" runat="server" Visible="true" />
</p> 

Solution

  • why dont you do the following?

    Page.Validate("save");
    if (Page.IsValid) 
    {
    //Continue with your logic
    }
    else
    {
    //Display errors, hide controls, etc.
    }
    

    This only fires your validation group and furthermore , you can use a validation summary to display your message about the correct formats of the text boxes.

    And you can display an error message then and there to display the correct format.