Reputation: 1604
I need a reg exp to check for telephone number, the telephone number goes like this:
400-1889-8989
So the telephone number should include 0-9 and dash(-) only, and it has to start and end with number. Don't know how to write a reg exp... any help would be appreciated! Thanks.
Upvotes: 0
Views: 180
Reputation: 148
Going by the thought that all telephone numbers would be of the form 000-0000-0000, that is 3 digits followed by -
, followed by 4 digits, then a -
again and another 4 digits.
The regex will be like this
/[0-9]{3}-[0-9]{4}-[0-9]{4}/
Upvotes: 0
Reputation: 769
This regex will only return true if the number is like "3-4-4", I mean "932-3434-4232" or "534-3342-4233"
^[0-9]{3}-[0-9]{4}-[0-9]{4}$
Upvotes: 1
Reputation: 97140
This is a strict interpretation of your requirements (include 0-9 and dash (-) only, and it has to start and end with number):
/^[0-9][0-9-]*[0-9]$/
Do realize that this leaves the door open to a variety of inputs that will not even remotely resemble a phone number, e.g. 00
, 0-----9
, 666666666666666666666666
and so on.
Upvotes: 2