2

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> 
2
  • what is your question
    – meda
    Commented Jul 7, 2014 at 14:28
  • @meda There are two questions in the original post.
    – Bhav
    Commented Jul 7, 2014 at 14:37

1 Answer 1

5

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.

3
  • @Bhav please accept as answer if it was what you wanted.
    – Rex
    Commented Jul 7, 2014 at 15:04
  • 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.
    – Bhav
    Commented Jul 7, 2014 at 15:24
  • @Bhav did you add a validation group to the validation summary used?
    – Rex
    Commented Jul 7, 2014 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.