1

what I'm wanting to do is add a new validation method that will validate if 2 fields are not equal DEPENDING if a radio button is ticked (see here - http://jsfiddle.net/dazziep/uVnQ4/9/)

The code I have works, but I have had to fudge it a little by adding the field in the actual method where I would rather pass it through.

So at the moment I have ...

jQuery.validator.addMethod("test", function(value, element, param){
   // check if dependency is met
   if (!this.depend(param, element)) return "dependency-mismatch";
   return value != $("#ImEqDepEq").val();
   },
   jQuery.format("Should not match Equality value.")
);`

I obviously just want ($("#ImEqDepEq").val()) to be another parameter that I pass in.

I hope this makes some kind of sense, I'm rubbish at explaining things :(

cheers

Daz

1 Answer 1

3

You can pass additional info to the rule using param option.

$("form").validate({
    rules: {
        ImEqDep: {
            test: {
                depends: function () {
                    return $("#ImEqDepRad1Yes").is(':checked');
                },
                param: '#ImEqDepEq'
            }
        }
    }
});

jQuery.validator.addMethod("test", function (value, element, param) {
    // check if dependency is met
    if (!this.depend(param, element)) return "dependency-mismatch";
    return value != $(param).val();
}, jQuery.format("Should not match Equality value."));

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

spot on, works a treat.. I can't believe how simple and obvious the solution was.. I actually feel a bit silly asking now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.