regex - Validate phone number with JavaScript -


i found code in website, , works perfectly. validates phone number in 1 of these formats:
(123) 456-7890 or 123-456-7890

the problem client (i don't know why, maybe client stuffs) wants add format, ten numbers consecutively, this: 1234567890.

i'm using regular expression,

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/ 

how can add validates format? i'm not regular expressions.

first off, format validator appropriate nanp (country code +1) numbers. application used phone number outside north america? if so, don't want prevent people entering valid [international] number.

secondly, validation incorrect. nanp numbers take form nxx nxx xxxx n digit 2-9 , x digit 0-9. additionally, area codes , exchanges may not take form n11 (end 2 ones) avoid confusion special services except numbers in non-geographic area code (800, 888, 877, 866, 855, 900) may have n11 exchange.

so, regex pass number (123) 123 4566 though not valid phone number. can fix replacing \d{3} [2-9]{1}\d{2}.

finally, feeling you're validating user input in web browser. remember client-side validation only convenience provide user; still need validate input (again) on server.

tl;dr don't use regular expression validate complex real-world data phone numbers or urls. use specialized library.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -