如何有效验证 Asp.Net 会员表单

发布于 2024-08-08 22:47:48 字数 6348 浏览 1 评论 0原文

用于有效验证注册和/或登录页面的其他建议和示例代码是什么?也许使用 jQuery?
例如,我想使用一些 ajax 来通知用户电子邮件地址正在使用中。

注册.aspx

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="CreateUserWizard1" />
<asp:CustomValidator id="CustomValidator1" runat="server" ValidationGroup="CreateUserWizard1"
    Display="None" EnableClientScript="False"></asp:CustomValidator>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" oncreatinguser="Createuserwizard1_CreatingUser">
  <WizardSteps>
      <asp:CreateUserWizardStep runat="server" >
          <ContentTemplate>
              <table border="0">
                  <tr>
                    <td align="center" colspan="2">
                            Sign Up for Your New Account</td>
                  </tr>
                  <tr>
                    <td align="right">
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                        </td>
                    <td>
                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                <tr>
                    <td align="right">
                            <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                        </td>
                    <td>
                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                ControlToValidate="Password" ErrorMessage="Password is required." 
                                ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                </tr>
                <tr>
                    <td align="right">
                            <asp:Label ID="ConfirmPasswordLabel" runat="server" 
                                AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                        </td>
                    <td>
                        <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                                ControlToValidate="ConfirmPassword" 
                                ErrorMessage="Confirm Password is required." 
                                ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                <tr>
                    <td align="right">
                        <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>                                
                        </td>
                    <td>
                        <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>                       
                <tr>
                   <td align="center" colspan="2">
                        <asp:CompareValidator ID="PasswordCompare" runat="server" 
                                ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                Display="Dynamic" 
                                ErrorMessage="The Password and Confirmation Password must match." 
                                ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                        </td>
                </tr>
                <tr>
                   <td align="center" colspan="2" style="color:Red;">
                        <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep runat="server" />
    </WizardSteps>
</asp:CreateUserWizard>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

注册.aspx.cs

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";
}

What are other suggestions and sample code for efficiently validating the Register and/or login page. Perhaps use of jQuery?
I would like to use some ajax to inform the user the email address is in use for example.

Register.aspx

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="CreateUserWizard1" />
<asp:CustomValidator id="CustomValidator1" runat="server" ValidationGroup="CreateUserWizard1"
    Display="None" EnableClientScript="False"></asp:CustomValidator>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" oncreatinguser="Createuserwizard1_CreatingUser">
  <WizardSteps>
      <asp:CreateUserWizardStep runat="server" >
          <ContentTemplate>
              <table border="0">
                  <tr>
                    <td align="center" colspan="2">
                            Sign Up for Your New Account</td>
                  </tr>
                  <tr>
                    <td align="right">
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                        </td>
                    <td>
                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                <tr>
                    <td align="right">
                            <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                        </td>
                    <td>
                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                ControlToValidate="Password" ErrorMessage="Password is required." 
                                ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                </tr>
                <tr>
                    <td align="right">
                            <asp:Label ID="ConfirmPasswordLabel" runat="server" 
                                AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                        </td>
                    <td>
                        <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                                ControlToValidate="ConfirmPassword" 
                                ErrorMessage="Confirm Password is required." 
                                ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                <tr>
                    <td align="right">
                        <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>                                
                        </td>
                    <td>
                        <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>                       
                <tr>
                   <td align="center" colspan="2">
                        <asp:CompareValidator ID="PasswordCompare" runat="server" 
                                ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                Display="Dynamic" 
                                ErrorMessage="The Password and Confirmation Password must match." 
                                ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                        </td>
                </tr>
                <tr>
                   <td align="center" colspan="2" style="color:Red;">
                        <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep runat="server" />
    </WizardSteps>
</asp:CreateUserWizard>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Register.aspx.cs

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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文