简体   繁体   中英

how to add NOTEqual To rule in jQuery.validation

I want to know how i can add jquery validation for not equal values in two input textbox. I have tried this Example but its not working for me.

Jquery Code:

$.validator.addMethod("notEqual", function (value, element, param) {
            return this.optional(element) || value != param;
        }, "cannot be same as oldpassword");

        $("#frmAddEdit").validate({
            rules: {
                "txtoldpassword": { required: true },
                "txtnpassword": { required: true, notEqual:"#txtoldpassword" },
            },
            messages: {
                "txtoldpassword": { required: "old Password is required" },
                "txtnpassword": { required: "New Password is required" },
            },
            submitHandler: function (form) {
                ChangeUserPassword();
                return false;
            }
        });

When i put alert on validation method param shows the same value (ie #txtoldpassword ) and when i put like this notEqual: $("#txtoldpassword").val() param becomes "" (blank)

Please help me on this.

I just changed the way of passing value of textbox for notEqual and it worked for me.

JQuery Code :

  $("#frmAddEdit").validate({
            rules: {
                "txtoldpassword": { required: true, noSpace: true },
                "txtnpassword": {
                    required: true, noSpace: true, notEqual: function () { return $("#txtoldpassword").val() },
                }
            },
            messages: {
                "txtoldpassword": { required: "old Password is required" },
                "txtnpassword": { required: "New Password is required" },
            },
            submitHandler: function (form) {
                ChangeUserPassword();
                return false;
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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