如何验证密码和确认密码

时间:2014-02-13 03:21:55

标签: javascript

有人可以帮我验证确认密码和密码。

这是我的代码:

<script>

// Form validation

$(function() {

// Setup form validation on the #register-form element
$("#register-form").validate({

    // Specify the validation rules
    rules: {
        firstname: "required",
        lastname: "required",
        gender: "required",
        email: "required",
        password: {
            required: true,
            minlength: 6
        },
        phone: {
            required: true,
            minlength: 11
        },

        agree: "required"
    },

    // Specify the validation error messages
    messages: {
        firstname: "Please enter your first name",
        lastname: "Please enter your last name",          
        password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 6 characters long"
        },
        phone: "Please enter a valid phone number",
        email: "Please enter your email",
        gender: "Please enter your gender",
        birthday: "Please enter your birthday",
        agree: "Please accept our policy"
    },

    submitHandler: function(form) {
        form.submit();
    }
});
});
</script>

1 个答案:

答案 0 :(得分:0)

您可以使用equalTo规则

$(function () {

    // Setup form validation on the #register-form element
    $("#register-form").validate({

        // Specify the validation rules
        rules: {
            firstname: "required",
            lastname: "required",
            gender: "required",
            email: "required",
            password: {
                required: true,
                minlength: 6,
                equalTo: '#confirmPassed'//confirm password selector
            },
            phone: {
                required: true,
                minlength: 11
            },

            agree: "required"
        },

        // Specify the validation error messages
        messages: {
            firstname: "Please enter your first name",
            lastname: "Please enter your last name",
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 6 characters long",
                equalTo: 'Confirm Password and Password must match'
            },
            phone: "Please enter a valid phone number",
            email: "Please enter your email",
            gender: "Please enter your gender",
            birthday: "Please enter your birthday",
            agree: "Please accept our policy"
        },

        submitHandler: function (form) {
            form.submit();
        }
    });
});