开发者

jquery validator addMethod question

开发者 https://www.devze.com 2023-01-31 19:47 出处:网络
Working with JQuery Validator and just have a quick question. I\'m using addMethod to define a custom validation for Australian phone numbers based on a regex. This is a method that I\'d like to use i

Working with JQuery Validator and just have a quick question. I'm using addMethod to define a custom validation for Australian phone numbers based on a regex. This is a method that I'd like to use in multiple pages, so I want to define the function in an external .js file, and pop it into the addMethod call.开发者_开发百科

If I use:

$.validator.addMethod('phone', regexPhone(value), 'Please enter a valid phone number');

I get a reference error: can't find value, but if you build the function straight into the addMethod call, you use the parameter value.

Is there any way to add a reusable function into the call for addMethod?

Ta, Ryan.


Try

$.validator.addMethod('phone', regexPhone, 'Please enter a valid phone number');

The second parameter in addMethod is a callback, This will call the function regexPhone.

The way you did called the regexPhone function before appending in the callback.

0

精彩评论

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