繁体   English   中英

jQuery验证确认密码

[英]jquery validate confirm password

我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/来验证允许用户更改其现有设置的表单。 该表单具有用于密码的字段以及用于确认密码的字段。 为了使密码保持不变,我指示用户将其保留为空白(也许是更直观的方式吗?),并且仅当用户更改其密码时才会显示确认密码字段。

它工作得很好,但是有两个问题。 首先,如果您在“密码”字段中输入内容并按Tab键,则不会转到“确认密码”字段,而是转到该字段之后的下一个字段。 其次,如果您在密码字段中输入内容并按Enter,则在检查确认密码之前提交表单。 出于某种原因,使用jsfiddle演示http://jsfiddle.net/4htKu/2/不会显示第二个缺陷,但是在第二个相同的演示http://tapmeister.com/test/validatePassword.html上却可以显示。 下面的代码与前面提到的两个演示相同。 我相信问题与确认密码被隐藏有关,但不确定。 我该怎么做才能解决这两个问题? 谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>jQuery validation plug-in - main demo</title>

        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function() {

                $("#confirm_password").parent().hide();
                $("#password").val('').
                blur(function() {
                    $t=$(this);
                    if($.trim($t.val())!="" && !$t.hasClass('error')) {$("#confirm_password").parent().show();}
                    else if($.trim($t.val())=="") {$("#confirm_password").val('').parent().hide();}
                });

                // validate signup form on keyup and submit
                $("#signupForm").validate({
                    rules: {
                        firstname: {required: true,minlength: 2},
                        lastname: {required: true,minlength: 2},
                        username: {required: true,minlength: 2},
                        password: {required: true,minlength: 2},
                        confirm_password: {required: true,minlength: 2, equalTo: "#password"}
                    },
                    messages: {},
                    submitHandler: function(form) {alert("submitted");}
                });

            });
        </script>

    </head>
    <body>

        <form class="cmxform" id="signupForm" method="get" action="">
            <fieldset>
                <legend>Validating a complete form</legend>
                <p>
                    <label for="firstname">Firstname*</label>
                    <input id="firstname" name="firstname" />
                </p>
                <p>
                    <label for="lastname">Lastname*</label>
                    <input id="lastname" name="lastname" />
                </p>
                <p>
                    <label for="username">Username*</label>
                    <input id="username" name="username" />
                </p>
                <p>
                    <label for="password">Password (Leave blank to remain unchanged)</label>
                    <input id="password" name="password" type="password" />
                </p>
                <p>
                    <label for="confirm_password">Confirm password</label>
                    <input id="confirm_password" name="confirm_password" type="password" />
                </p>
                <p>
                    <label for="other">Other Non-required</label>
                    <input id="other" name="other" />
                </p>
                <p>
                    <input class="submit" type="submit" value="Submit" />
                </p>
            </fieldset>
        </form>

    </body>
</html>

代替使用.blur ,而使用.keydownsettimeout来检测该值是否为空。 如果不是空白,请显示它,否则将其隐藏。

$("#password").keydown(function() {
    var self = this, $self = $(this);
    setTimeout(function(){
        if ( $.trim(self.value) != "" && !$self.is(".error") ) {
            $("#confirm_password").parent().show();
            return;
        }
        $("#confirm_password").val('').parent().hide();
    },0);
});

至于按回车键,这不是应该做的吗? 提交表格?

使用.blur的原因是您在显示隐藏字段之前先.blur下一个字段。 此代码解决了该问题。

为什么不问用户是否要通过复选框更改密码? 如果选中,则显示密码字段,如果不允许,则提交表单。

其他HTML:

<p>
    <label for="passwordChangeYes">Check if you want to change your password</label>
    <input type="checkbox" name="passwordChangeYes" id="passwordChangeYes" value="Yes" />
</p>

附加JS:

$("#password").hide();
$('#passwordChangeYes').one("click", function () {
    if ($('#passwordChangeYes').is(':checked')) {
            $("#password").fadeIn(400);
        }
    });

我不确定如何从复选框单击的验证部分中删除或添加所需的部分。 但这也许是一个起点吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM