无法使用jquery确认密码验证

时间:2017-03-01 17:58:52

标签: jquery jquery-validate

我有一个简单的确认密码jquery脚本,它根本不起作用... 每个长度的每个密码都会验证表单。

这是表格

<form action="${updatePassword}" method="post" class="form-signin" id="form-signin">

    <div class="form-group">
        <input type="password" id="password" name="password" placeholder="Enter new Password..." class="form-password form-control">
    </div>
    <div class="form-group">
        <input type="password" name="confirmpassword" placeholder="Confirm Password..." class="form-password form-control" id="confirmpassword">
    </div>
    <button type="submit" class="btn" id="submit" name="submit> Reset password</button>
</form>

这是脚本:

$(function () {
    $("#submit").click(function ()  //input button click
    {
        $.validator.setDefaults({
            debug: true,
            success: "valid"
        });

        $("#form-signin").validate({
            rules: {
                password: {
                    required: true,
                    minlength: 5

                },

                confirmpassword: {
                    equalTo: "#password"
                }

            },
            messages: {
                password: {
                    required: "Inserire una password",
                    minlength: "La password deve contenere almeno 5 caratteri"
                },
                confirmpassword: {
                    equalTo: "Le due password devono coincidere"
                }
            }

        });
    });
});

看起来非常线性......

2 个答案:

答案 0 :(得分:2)

看起来您需要附加到点击处理程序。

删除包裹$("#submit").click(function () {

所以你的代码看起来像是:

$(function () {
    $.validator.setDefaults({
        debug: true,
        success: "valid"
    });

    $("#form-signin").validate({
        // ...
    });
});

答案 1 :(得分:1)

当提交按钮类型时,无需检查点击事件。如果您使用了jquery验证插件,表单将自动验证。

修改你的代码,如belew:

                 $("#form-signin").validate({
               rules: {
                   password: { 
                     required: true,
                     minlength: 5

                   } , 

                       confirmpassword: { 
                        equalTo: "#password"
                   }

               },
               messages: {
                   password: {
                       required: "Inserire una password",
                       minlength: "La password deve contenere almeno 5 caratteri"
                   },
                   confirmpassword: {
                       equalTo: "Le due password devono coincidere"
                   }
               }

           });

这是Working Code