开发者

Is it possible to override jquery.validates various validation methods?

I'd like to override the "number" validation of the jquery validate plugin.

This part:

// http://docs.jquery.com/Plugins/Validation/Methods/number
number: function(value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
},

I did some searching but all that I could find was how to override the messages (which I've done already) and how to over ride core jQuery functionality, which pointed me in this direction...

$.fn.validate.prototype.methods.number = function (value, element) {
    return this.optional(eleme开发者_如何学编程nt) || /^-?\d+(?:\,\d+)?$/.test(value);
};

With this code I get the following error:

$.fn.validate.prototype.methods is undefined

Am I missing something? Or am I trying to do the impossible?

If this is impossible, suggestions for an alternate way to change this functionality, with out having to add custom validators to every single number field in my app, would be most welcome!

Thanks!


Yes, this functionality is built in to the library, so you can do it without having to deal with the internals.

$.validator.addMethod("number", function(value, element) {
    return this.optional(element) || /^-?\d+(?:\,\d+)?$/.test(value);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜