2

I'm using the Jquery Mask plugin to validate a phone text input like so: http://igorescobar.github.io/jQuery-Mask-Plugin/

$('#phone').mask('(999) 999-9999');

How can I use a regex expression to validate this phone number format?

Thank you!

HandiworkNYC.com
  • 10,914
  • 25
  • 92
  • 154
  • However, don't forget to implement [server-side validation](http://stackoverflow.com/questions/162159/javascript-client-side-vs-server-side-validation) as well! – T3 H40 Nov 05 '15 at 06:50

1 Answers1

3

Maybe something like that? (I'm not sure of the syntax, so correct me if I'm wrong)

function checkPhoneNumber(num) {
    var regexp = /\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}/; 
    var valid = regexp.test(num);
    return valid;
}

\( This is for an opening bracket;

[0-9]{3} This is for digits from 0 through 9 occurring only 3 times;

\) This is for a closing bracket;

\s This is for a white space character;

- This is for the dash

[0-9]{4} This is for digits from 0 through 9 occurring only 4 times;

Jerry
  • 70,495
  • 13
  • 100
  • 144