如何有效验证 Asp.Net 会员表单
用于有效验证注册和/或登录页面的其他建议和示例代码是什么?也许使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用了此处描述的技术 http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/ 验证/
I ended up using techniques described here http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/ to validate/