0

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.

1 Answer 1

1

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.

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

1 Comment

Thanks! As soon as I saw your response, it made sense. Been fiddling with validator for the last 4 or 5 hours, so I'm a little bit frazzled! =)

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.