1

I'm trying to increase compatability between jQuery validation and knockout validation plugins so that the same rules (admittedly a subset of functionality) can be used on both in different places in my application.

One of the areas to tackle is that jQuery validation has different capitalization for minlength, maxlength etc (KO uses minLength). I'd hoped to be able to create a simple alias...

    $.validator.addMethod("minLength", function(value, element, params) {
        return $.validator.methods.minlength(value, element, params);
    });

which seems sufficiently simple to bridge the gap. However, this throws "this.optional is not a function" when it hits the jQuery validator here

 // http://docs.jquery.com/Plugins/Validation/Methods/minlength
 minlength: function(value, element, param) {
    return this.optional(element) || this.getLength($.trim(value), element) >= param;
 },

I'm guessing I need to pass the correct "this" context in somehow?

1 Answer 1

1

Just figured it out...

    $.validator.addMethod("minLength", function(value, element, params) {
        return $.validator.methods.minlength.call(this,value, element, params);
    });

calls the validator method and passes in the correct "this" context. Hope this is useful.

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

Comments

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.