密码自定义验证不起作用

发布于 2024-09-16 13:38:10 字数 1233 浏览 9 评论 0原文

我有一张登记表格。 我正在检查密码是否有 6 个字符,如下所示:

<input type="password" runat="server" name="password" size="41" maxlength="64" id="txtpassword" /><span>*</span>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Fill in a password." ControlToValidate="txtpassword"></asp:RequiredFieldValidator>
                    &nbsp;
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="valPassword" ErrorMessage="Password must be 6 characters long." ControlToValidate="txtpassword"></asp:CustomValidator>&nbsp;(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 技术交流群。

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

发布评论

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

评论(2

终陌 2024-09-23 13:38:10

尝试将 txtpassword 更改为 而不是输入。

try changing txtpassword to an <asp:Textbox> instead of an input.

甜中书 2024-09-23 13:38:10

也许是名称与 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.

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