Alon B
Alon B

Reputation: 155

javascript regular expression phone number validation

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 :)

Upvotes: 0

Views: 9469

Answers (4)

nobody
nobody

Reputation: 10645

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

Upvotes: 1

Paul Creasey
Paul Creasey

Reputation: 28894

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

Upvotes: 2

Alex Turpin
Alex Turpin

Reputation: 47776

That's my take:

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

Upvotes: 0

user578895
user578895

Reputation:

/^\d{3}-\d{7}$/.test( phone_number );

Upvotes: 3

Related Questions