0

I need to compare two input text fields of a form using a custom rule for jQuery Validate.

I tried the following code

//Our validation script will go here.

$(document).ready(function(){
     //custom validation rule - text only
     $.validator.addMethod("NotEqualTo", 
             function(value, element) {
                 return ???;
             }, "Two Properties are matching"
    );

    $("#frmUpdatePassword").validate({
        onfocusout: true,
        rules: {
            NewPassword: {
                NotEqualTo: '#OldPassword'                }
        },
        messages: {
            NewPassword: {
                NotEqualTo: "Two Properties are having same values"
            }
        }
    });
});

The HTML TextBox is

<input type="password" id="OldPassword" name="OldPassword" />
<input type="password" id="NewPassword" name="NewPassword" />

Kindly assist me how to compare for not equal of two properties in a generic manner not only for comparing these two fields.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • @DarrenSweeney Here I will pass the property ID instead of value... –  Jun 21 '17 at 06:53

2 Answers2

1

The Custom Validation Rule should be

$.validator.addMethod('NotEqualTo', function (value, element, param) {

       var str1 = value  || '';
       var str2 = $(param).val() || '';

       return $.trim(str1) != $.trim(str2);

}, 'Invalid: Two values are matching');

The Rule should be

rules: {
    NewPassword: {
        NotEqualTo: '#OldPassword'
    }
}
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • I'm getting an error `TypeError: settings[eventType].call is not a function` –  Jun 21 '17 at 07:19
  • @Mastero, you should never set `onfocusout: true`. Read the docs, "`true` is not a valid value" and could break the built-in functionality. If you want `onfocusout` enabled, simply leave this option out and that's already the default. This is the source of your error. – Sparky Jun 21 '17 at 16:49
-1

Please try this, I had face same issue and resolve by below mentionion code.

//Our validation script will go here.
$(document).ready(function(){
     //custom validation rule - text only
     $.validator.addMethod("NotEqualTo", function(value, element) {

        var OldPassword = $("#OldPassword").val() || null;

        if(value == OldPassword)
        {
            return true; //Error fire!
        }
        else
        {
            return false; //Error Not fire!
        }
     }, "Two Properties are matching");

    $("#frmUpdatePassword").validate({
        onfocusout: true,
        rules: {
            NewPassword: {
                NotEqualTo: true;
            }
        },
        messages: {
            NewPassword: {
                NotEqualTo: "Two Properties are having same values"
            }
        }
    });
});

Your over all code is fine! just update validation method.

Binary Brackets
  • 494
  • 1
  • 4
  • 12
  • I'm getting an error `TypeError: settings[eventType].call is not a function` –  Jun 21 '17 at 07:17
  • Okay..., It's related to JQuery version issue, Please refer link: https://github.com/jquery-validation/jquery-validation/issues/428, and update the `onfocusout` param from the validation plugin setup. also try to:https://stackoverflow.com/questions/15103289/jquery-validate-js-onkeyup-true-error – Binary Brackets Jun 21 '17 at 07:26
  • Your answer is wrong. Use `return false` to trigger a validation error, not `true`. – Sparky Jun 21 '17 at 16:34