admin管理员组

文章数量:1322535

I would like to test if the right phone number was enterred in the text field. The phone number should be ddd-ddddddd which means 3digits then must have "-" and then 7 digits. How do I set the regular expression for that ? Thanks :)

I would like to test if the right phone number was enterred in the text field. The phone number should be ddd-ddddddd which means 3digits then must have "-" and then 7 digits. How do I set the regular expression for that ? Thanks :)

Share Improve this question asked Aug 2, 2011 at 20:30 Alon BAlon B 1554 silver badges13 bronze badges 4
  • 2 Exact duplicate of stackoverflow./questions/1736686/… – Lance Commented Aug 2, 2011 at 20:32
  • I hate sites that require exact formats like that. It's so easy to scan through the digits alone and then add the dashes as necessary. /rant – Chris Gregg Commented Aug 2, 2011 at 20:34
  • 1 The format asked for is not the same. – Alex Turpin Commented Aug 2, 2011 at 20:35
  • I hate sites that require exact formats that only apply to one country. (Unless they make it really clear on the front page that they only deal with residents of that country, and even then - why?) /rant – nnnnnn Commented Aug 3, 2011 at 0:07
Add a ment  | 

4 Answers 4

Reset to default 3
/^\d{3}-\d{7}$/.test( phone_number );
var phoneNumber = '123-1234567';
if(phoneNumber.match(/^\d{3}-\d{7}$/))
{
   alert('blah');
}

Exactly as you say it /^\d\d\d-\d\d\d\d\d\d\d$/

That's my take:

/^[0-9]{3}\-[0-9]{7}$/

本文标签: javascript regular expression phone number validationStack Overflow