title: How to effectively validate Asp.Net membership forms
body: <p>What are other suggestions and sample code for efficiently validating the Register and/or login page. Perhaps use of jQuery?<br>
  I would like to use some ajax to inform the user the email address is in use for example.</p>
  
  <p>Register.aspx</p>
  
  <pre><code>&lt;asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="CreateUserWizard1" /&gt;
  &lt;asp:CustomValidator id="CustomValidator1" runat="server" ValidationGroup="CreateUserWizard1"
      Display="None" EnableClientScript="False"&gt;&lt;/asp:CustomValidator&gt;
  &lt;asp:CreateUserWizard ID="CreateUserWizard1" runat="server" oncreatinguser="Createuserwizard1_CreatingUser"&gt;
    &lt;WizardSteps&gt;
        &lt;asp:CreateUserWizardStep runat="server" &gt;
            &lt;ContentTemplate&gt;
                &lt;table border="0"&gt;
                    &lt;tr&gt;
                      &lt;td align="center" colspan="2"&gt;
                              Sign Up for Your New Account&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                      &lt;td align="right"&gt;
                          &lt;asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"&gt;User Name:&lt;/asp:Label&gt;
                          &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="UserName" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                  ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                  ToolTip="User Name is required." ValidationGroup="CreateUserWizard1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td align="right"&gt;
                              &lt;asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"&gt;Password:&lt;/asp:Label&gt;
                          &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="Password" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                          &lt;asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                  ControlToValidate="Password" ErrorMessage="Password is required." 
                                  ToolTip="Password is required." ValidationGroup="CreateUserWizard1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                          &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td align="right"&gt;
                              &lt;asp:Label ID="ConfirmPasswordLabel" runat="server" 
                                  AssociatedControlID="ConfirmPassword"&gt;Confirm Password:&lt;/asp:Label&gt;
                          &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                          &lt;asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                                  ControlToValidate="ConfirmPassword" 
                                  ErrorMessage="Confirm Password is required." 
                                  ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td align="right"&gt;
                          &lt;asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email"&gt;E-mail:&lt;/asp:Label&gt;                                
                          &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="Email" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                  ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                  ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;                       
                  &lt;tr&gt;
                     &lt;td align="center" colspan="2"&gt;
                          &lt;asp:CompareValidator ID="PasswordCompare" runat="server" 
                                  ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                  Display="Dynamic" 
                                  ErrorMessage="The Password and Confirmation Password must match." 
                                  ValidationGroup="CreateUserWizard1"&gt;&lt;/asp:CompareValidator&gt;
                          &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                     &lt;td align="center" colspan="2" style="color:Red;"&gt;
                          &lt;asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"&gt;&lt;/asp:Literal&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/ContentTemplate&gt;
          &lt;/asp:CreateUserWizardStep&gt;
          &lt;asp:CompleteWizardStep runat="server" /&gt;
      &lt;/WizardSteps&gt;
  &lt;/asp:CreateUserWizard&gt;
  &lt;asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /&gt;
  </code></pre>
  
  <p>Register.aspx.cs</p>
  
  <pre><code>protected void Createuserwizard1_CreatingUser(object sender, LoginCancelEventArgs e)
  {
      CreateUserWizard1.UserName.ToLower();
      // Check if the user name exists or email is in use.
      CreateUserWizard cuw = (CreateUserWizard)sender; 
      cuw.Email = cuw.UserName;
  
      TextBox passwordTextBox = 
          (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Password");
      TextBox confirmPasswordTextBox = 
          (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ConfirmPassword");
  
      CustomValidator1.IsValid = false;
      CustomValidator1.ErrorMessage = "I think the email is in use.";
      // Save password in the textbox
      passwordTextBox.Attributes.Add("Value", cuw.Password);
      passwordTextBox.Attributes.Add("Value", cuw.ConfirmPassword);
  
      e.Cancel = true;
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
      CustomValidator1.IsValid = false;
      CustomValidator1.ErrorMessage = "Hello errors";
  }
  </code></pre>