密码比较

时间:2017-12-29 09:55:06

标签: javascript laravel

我的代码存在很大问题。在注册时,我比较我的passowrd和他显示的密码不匹配。这是代码

{!! Form::open(array('url' => 'auth/postregister', 'class' => 'horizontal-form', 'id' => 'formRegister', 'method' => 'POST')) !!}
    {{ csrf_field() }}
                <label>Email</label>
                {!! Form::text('u_email', null, array('placeholder' => 'youremail@domaine.com', 'class' => 'form-group', 'id' => 'u_email', 'required')) !!}
                <label>Mot de passe</label>
                {!! Form::password('pwd1', null, array('placeholder' => '********', 'class' => 'form-group', 'id' => 'pwd1', 'required')) !!}

                <label>Confirmer votre mot de passe</label>
                {!! Form::password('pwd2', null, array('placeholder' => '********', 'class' => 'form-group', 'id' => 'pwd2', 'required')) !!}
                <p id="message"></p>

                <button type="submit" id="subscribe" class="btn btn-danger">S'enregistrer</button>
{!! Form::close() !!}
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/additional-methods.min.js"></script>
<script>
$("#formRegister").validate({
  rules: {
    pwd1: "required",
    pwd2: {
      equalTo: "#pwd1"
    }
  }
});
</script>

在密码确认输入为红色且后期数据未通过。 你有个主意吗

1 个答案:

答案 0 :(得分:0)

您可以扩展jQuery验证器以添加您的规则:

<script>

    jQuery.validator.addMethod( 'passwordMatch', function(value, element) {

        // The two password inputs
        var password = $("#pwd1").val();
        var confirmPassword = $("#pwd2").val();

        // Check for equality with the password inputs
        if (password != confirmPassword ) {
            return false;
        } else {
            return true;
        }

    }, "Your Passwords Must Match");

    $("#formRegister").validate({
        rules: {
            pwd1: {
                required: true,
                minlength: 5
            },
            pwd2: {
                required: true,
                minlength: 5,
                passwordMatch: true // the added rule
            }
        },

        // If you want some custome messages :)
        messages: {
            pwd1: {
                required: "What is your password?",
                minlength: "Your password must contain more than 5 characters"
            },
            pwd2: {
                required: "You must confirm your password",
                minlength: "Your password must contain more than 5 characters",
                passwordMatch: "Your Passwords Must Match" // custom message for mismatched passwords
            }
        }
    });
</script>

Ps:source