title: Validation on a button click using RequiredFieldValidator
body: <p>In the past, on button click events, I've validated without using RequiredFieldValidators.  However, I thought I'd learn about them and implement them.</p>
  
  <p>My old approach:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>However, using RequiredFieldValidators with the same example, am I correct in saying that I don't have to check again <code>if (txtSubject.Text.Equals("") || txtEmail.Text.Equals("") || txtComments.Text.Equals(""))</code> like below or is it good practice to do so?</p>
  
  <pre><code>protected void btnSubmit_Click(object sender, EventArgs e)
  {
      if (Page.IsValid)
      {
          //...further validation statements e.g. check lengths
          try
          {
              SendMail();
          }
          catch (Exception)
          {
          }
      }
  }
  </code></pre>
  
  <p>If I should still include the line, it should go at the beginning of the if (Page.IsValid), right?</p>
  
  <p>HTML code:</p>
  
  <pre><code>&lt;p&gt;Contact Form&lt;/p&gt;
  &lt;p&gt;
      Your name:
      &lt;asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="*"
          ControlToValidate="txtName" ValidationGroup="save" /&gt;&lt;br /&gt;
      &lt;asp:TextBox ID="txtName" runat="server" Width="250px" /&gt;&lt;br /&gt;
      Your email address:
      &lt;asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="*"
          ControlToValidate="txtEmail" ValidationGroup="save" /&gt;&lt;br /&gt;
      &lt;asp:TextBox ID="txtEmail" runat="server" Width="250px" /&gt;
      &lt;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" /&gt;&lt;br /&gt;
      Subject:
      &lt;asp:RequiredFieldValidator ID="rfvSubject" runat="server" ErrorMessage="*"
          ControlToValidate="txtSubject" ValidationGroup="save" /&gt;&lt;br /&gt;
      &lt;asp:TextBox ID="txtSubject" runat="server" Width="400px" /&gt;&lt;br /&gt;
      Comments:
      &lt;asp:RequiredFieldValidator ID="rfvComments" runat="server" ErrorMessage="*"
          ControlToValidate="txtComments" ValidationGroup="save" /&gt;&lt;br /&gt;
      &lt;asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" /&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" ValidationGroup="save" /&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;asp:Label ID="lblMessage" runat="server" Visible="true" /&gt;
  &lt;/p&gt; 
  </code></pre>