Validation on a button click using RequiredFieldValidator

14,116

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.

Share:
14,116
Bhav
Author by

Bhav

DAY JOB: C#, .Net devloper.

Updated on June 14, 2022

Comments

  • Bhav
    Bhav almost 2 years

    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 protected]" 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> 
    
  • Rex
    Rex almost 10 years
    @Bhav please accept as answer if it was what you wanted.
  • Bhav
    Bhav almost 10 years
    Why doesn't the following control output the message 'You must enter...'? <asp:ValidationSummary id="valSum" DisplayMode="BulletList" EnableClientScript="true" HeaderText="You must enter the following fields:" runat="server"/> ...I've changed the ErrorMessage and Text properties accordingly.
  • Rex
    Rex almost 10 years
    @Bhav did you add a validation group to the validation summary used?