hi..
10-digit US phone number such as 123-456-6789
whn i want to check this number using regular expression. i found the expression coding whicis "[0-9]{3}-[0-9]{3}-[0-9]{4}". .. But the problem is whn i type first character 4 its says thats nt an error.
Eg: 1234-456-6789
123-4567-789 those are works...
bt i want to check only 3 character.. hows that.?
Regards
AlanPosted Jan 29, 2008, 12:42 PM
The reason why "1234-456-6789" works is because it contains the sub-string "234-456-6789" which does satisfy the regular expression. To prevent this, include the beginning of line (^) and end of line ($) metacharacters in the expression, viz:
"^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
However, "123-4567-789" shouldn't work and didn't in fact work when I tried it myself.
Having said all that, the expression used by Manish is the one which is normally used as it can validate all the following formats amongst others:
(425) 555-0123
425-555-0123
425 555 0123
1-425-555-0123
Blocked AccountPosted Jan 29, 2008, 8:17 AM
Use this regular expression
^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
Happy Coding!!