Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Created May 31, 2017 04:56
Show Gist options
  • Save hengkiardo/eb6e917ea5c8ea8038d872c3b1a9277c to your computer and use it in GitHub Desktop.
Save hengkiardo/eb6e917ea5c8ea8038d872c3b1a9277c to your computer and use it in GitHub Desktop.
Custom method to jQuery Validation Plugin for Phone Numbers
jQuery.validator.addMethod("phoneno", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/);
}, "<br />Please specify a valid phone number");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment