开发者

Jquery form validation making password fields require if any one is having any value

开发者 https://www.devze.com 2023-04-03 06:46 出处:网络
I have two password fields pwd1 and pwd2 I am using jquery plugin http://bassistance.de/jquery-plugins/jquery-plugin-validati开发者_如何学Goon/

I have two password fields

pwd1 and pwd2

I am using jquery plugin http://bassistance.de/jquery-plugins/jquery-plugin-validati开发者_如何学Goon/

How to make pwd1 and pwd2 to be required and equal if and only if any one of the field is non-empty ?


You can use an equalTo rule, as @voigtan suggests, but store the required rule in the element's class. That will allow you to enable or disable the rule at will:

var $passwords = $("input:password");
$passwords.change(function() {
    if ($passwords.filter(function() {
        return $.trim($(this).val()).length > 0;
    }).length > 0) {
        $passwords.addClass("required");
    } else {
        $passwords.removeClass("required");
    }
});
$("form").validate({
    rules: {
        "confirm-password": {
            equalTo: "#password"
        }
    }
});

You can test it in this fiddle.


Havent used it but: http://jquery.bassistance.de/validate/demo/ have the following:

rules: {
    //...
    password: {
        required: true,
        minlength: 5
    },
    confirm_password: {
        required: true,
        minlength: 5,
        equalTo: "#password"
    },
    //...
}

in the rule object


I think you need an additional method to do that! See http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/additional-methods.js

0

精彩评论

暂无评论...
验证码 换一张
取 消