1

So in my check phone number function, I have: the variable pn represents the textfield where the client inputs the phone number.

The desired format is: 000-0000 (obviously not only zeroes, but any digit 0-9).

How can I use regex to validate that it is in the desired format, and give an alert if it isn't? This is what i have

var pn = document.getElementById('ph').value;
//var vd = pn.search?

//if(not valid, alert("Not a valid number!");
4

1 回答 1

3

The desired format is: 000-0000

然后下面的正则表达式应该工作:

/^\d{3}-\d{4}$/

测试:

re = /^\d{3}-\d{4}$/;

re.test('123-4567'); // true
re.test('123-45678'); // false
于 2013-11-08T19:38:02.327 回答