$.validator.addMethod("phoneVal", function (number, element) {
number = number.replace(/\s+/g, "");
if (number.length == 12 && number.match(/^([0-9]{3})\-([0-9]{3})\-([0-9]{4})$/)) return true;
else return false;
}, "Please specify a valid phone number");
/*
^([0-9]{3})\-([0-9]{3})\-([0-9]{4})$
^ # Assert position at the beginning of the string.
\( # Match a literal "("...
? # between zero and one time.
( # Capture the enclosed match to backreference 1...
[0-9] # Match a digit...
{3} # exactly three times.
) # End capturing group 1.
\) # Match a literal ")"...
? # between zero and one time.
[-. ] # Match one character from the set "-. "...
? # between zero and one time.
⋯ # [Match the remaining digits and separator.]
$ # Assert position at the end of the string.
*/
$('#form').validate({
rules: {
phone: {
phoneVal: true
}
},
submitHandler: function () {
$('div').text('done');
},
invalidHandler: function () {
$('div').text('not');
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.