0

I want a validation method which will check that two fields are equal then hide one of them. At the moment I use equalTo to check they are equal. So I planned to add a validation method which delegates to equalTo and hides the field depending on the result.

The problem I have is that I cannot work out how to call equalTo on the correct instance of the validator. When I call it, I always see an error in equalTo, "this.settings is not defined". This is equalTo

 // http://docs.jquery.com/Plugins/Validation/Methods/equalTo
        equalTo: function( value, element, param ) {
            // bind to the blur event of the target in order to revalidate whenever the target field is updated
            // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
            var target = $(param);
//error here   
            if ( this.settings.onfocusout ) {
                    target.unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
                        $(element).valid();
                    });
                }
                return value === target.val();
        },

Here is my code.

// validate form
$.validator.addMethod("equalToAndHide", function(value, element, param) {
   //this does not work
   var result = $.validator.methods.equalTo.call(value, element, param);
   if(result === true){
        $(element).hide();
   }
   return result; 
 }, $.validator.format("Values must be equal_???"));

 $('form').validate({
                        rules : {

                        confirmEmail : {
                            equalToAndHide : utils.byId("alternateEmail")
                        }
                    },
                    submitHandler : function(form) {
                        accordionAjax.post($(form));
                    }
                });

I would prefer to leave equalTo as it is rather than override it and amend the call to this.settings, if possible.

1 Answer 1

2

You don't need to call the original equalTo from within your custom method... just write a whole new function using the original as your basis.

This is the default equalTo function...

function( value, element, param ) {
    var target = $(param);
    if ( this.settings.onfocusout ) {
        target.unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
            $(element).valid();
        });
    }
    return value === target.val();
}

Put it inside your custom method and tweak it to suite your needs...

$.validator.addMethod("equalToAndHide", function(value, element, param) {
        var target = $(param);
        if ( this.settings.onfocusout ) {
            target.unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
                $(element).valid();
            });
        }
        return value === target.val();
        $('#whatever').hide();
}, $.validator.format("Values must be equal_???"));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that will work. However I would prefer to use the real equalTo function if possible rather than fork it. That way I will get the benefit of any improvements when the validation plugin is upgraded. Since there's a TODO in the equalTo function I think that's fairly likely to happen.
@MarkChorley, This is your best option. I've answered hundreds of SO questions about the jQuery Validate plugin and read hundreds more. I've never seen it done the way you're requesting simply because it's not possible.
Thanks for your help. It is possible to call a jQuery Validate method like this because I am doing so when I get the error. The only problem is resolving the settings reference: were that not present in equalTo then my code would work. See this question for a similar example: stackoverflow.com/questions/7392875/… and this one for some information on instances of the Validator: stackoverflow.com/questions/1510165/…
@MarkChorley, thank-you, I've never seen this undocumented feature before. Regardless, I don't think I'd worry about equalTo changing so much in future versions... your custom method should still work.

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.