密码自定义验证不起作用
我有一张登记表格。 我正在检查密码是否有 6 个字符,如下所示:
<input type="password" runat="server" name="password" size="41" maxlength="64" id="txtpassword" /><span>*</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Fill in a password." ControlToValidate="txtpassword"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="valPassword" ErrorMessage="Password must be 6 characters long." ControlToValidate="txtpassword"></asp:CustomValidator> (at least 6 characters)
codebehind
protected void valPassword(object source, ServerValidateEventArgs args)
{
args.IsValid = ValidatePassword(args.Value);
}
private bool ValidatePassword(string pw)
{
if (pw.Length >= 6)
{
return true;
}
else
{
return false;
}
}
如果我让RequiredFieldValidator 和CustomValidator 一起工作并且我填写1 个字符,则表单将被接受。
如果我删除RequiredFiekdValidator并填写表单,则表单被接受,密码字段中根本没有字符
如果我离开CustomValidator并填写1个字符,则表单被接受
我的CustomValidator无法正常工作,我该怎么办丢失的 ?
I have a form for registration.
I'm checking if the password has 6 characters like this :
<input type="password" runat="server" name="password" size="41" maxlength="64" id="txtpassword" /><span>*</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Fill in a password." ControlToValidate="txtpassword"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="valPassword" ErrorMessage="Password must be 6 characters long." ControlToValidate="txtpassword"></asp:CustomValidator> (at least 6 characters)
codebehind
protected void valPassword(object source, ServerValidateEventArgs args)
{
args.IsValid = ValidatePassword(args.Value);
}
private bool ValidatePassword(string pw)
{
if (pw.Length >= 6)
{
return true;
}
else
{
return false;
}
}
If I leave the RequiredFieldValidator and the CustomValidator work together and I fill in 1 character , the form is accepted.
If I delete the RequiredFiekdValidator and fill in the form, the form is accepted with no character at all in the passwordfield
If I leave the CustomValidator and I fill in 1 character , the form is accepted
My CustomValidator isn't working properly, what am I missing ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 txtpassword 更改为
而不是输入。try changing txtpassword to an
<asp:Textbox>
instead of an input.也许是名称与 ID 的问题?我通常尝试让元素的名称和 id 匹配以避免混淆。
maybe a problem with name vs id? I usually try to have the name and the id of an element match to avoid confusion.