Regular expression for validating phone number in javascript


20-Dec-2019 09:12

:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(? When you match,

:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(? When you match, $1 contains the area code, $2 and $3 contain the phone number, and $5 contains the extension. Do you foresee any need to allow square, curly, or angled brackets for some regions? If you want to maintain per digit rules (such as in US Area Codes and Prefixes (exchange codes) must fall in the range of 200-999) well, good luck to you. I came up with this: Here's a perl script to test it. It should be compatible with international numbers and localization formats.

||

:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(? When you match, $1 contains the area code, $2 and $3 contain the phone number, and $5 contains the extension. Do you foresee any need to allow square, curly, or angled brackets for some regions? If you want to maintain per digit rules (such as in US Area Codes and Prefixes (exchange codes) must fall in the range of 200-999) well, good luck to you.

I came up with this: Here's a perl script to test it. It should be compatible with international numbers and localization formats.

How are extensions (e.g., 111-222-3333 x 44444) going to be handled? Even if you could write a big, hairy validator that would allow all the different legitimate formats, it would end up allowing pretty much anything even remotely resembling a phone number in the first place.

Here's a regex for a 7 or 10 digit number, with extensions allowed, delimiters are spaces, dashes, or periods: ^(? Honestly, I would recommend you don't try to validate phone numbers.

Since you'd need a regex anyway, you might as well have the regex do all the work. - ( 351) 282 43 50 50 - 90191919908 - 555-8909 - 001 6867684 - 001 6867684x1 - 1 (234) 567-8901 - 1-234-567-8901 x1234 - 1-234-567-8901 ext1234 - 1-234 567.89/01 ext.1234 - 1(234)5678901x1234 - (123)8575973 - (0055)(123)8575973 I answered this question on another SO question before deciding to also include my answer as an answer on this thread, because no one was addressing how to require/not require items, just handing out regexs: Regex working wrong, matching unexpected things From my post on that site, I've created a quick guide to assist anyone with making their own regex for their own desired phone number format, which I will caveat (like I did on the other site) that if you are too restrictive, you may not get the desired results, and there is no "one size fits all" solution to accepting all possible phone numbers in the world - only what you decide to accept as your format of choice. Note that it doesn't have any special rules for how many digits, or what numbers are valid in those digits, it just verifies that only digits, parenthesis, dashes, plus, space, pound, asterisk, period, comma, or the letters are present.

Take, for instance, my test script that downloads a web page and extracts all phone numbers using the regex. # my $external_filename = ' G-spring05.txt'; my @external_file = `curl $external_filename`; foreach my $line (@external_file) Edit: You can change \W* to \s*\W? I wasn't thinking of the regex in terms of, say, validating user input on a form when I wrote it, but this change makes it possible to use the regex for that purpose.

The international accounts for an optional initial ' ' and country code. Valid matches would be: My gut feeling is reinforced by the amount of replies to this topic - that there is a virtually infinite number of solutions to this problem, none of which are going to be elegant.

contains the area code, and contain the phone number, and contains the extension. Do you foresee any need to allow square, curly, or angled brackets for some regions? If you want to maintain per digit rules (such as in US Area Codes and Prefixes (exchange codes) must fall in the range of 200-999) well, good luck to you.

I came up with this: Here's a perl script to test it. It should be compatible with international numbers and localization formats.

How are extensions (e.g., 111-222-3333 x 44444) going to be handled? Even if you could write a big, hairy validator that would allow all the different legitimate formats, it would end up allowing pretty much anything even remotely resembling a phone number in the first place.

Here's a regex for a 7 or 10 digit number, with extensions allowed, delimiters are spaces, dashes, or periods: ^(? Honestly, I would recommend you don't try to validate phone numbers.

Since you'd need a regex anyway, you might as well have the regex do all the work. - ( 351) 282 43 50 50 - 90191919908 - 555-8909 - 001 6867684 - 001 6867684x1 - 1 (234) 567-8901 - 1-234-567-8901 x1234 - 1-234-567-8901 ext1234 - 1-234 567.89/01 ext.1234 - 1(234)5678901x1234 - (123)8575973 - (0055)(123)8575973 I answered this question on another SO question before deciding to also include my answer as an answer on this thread, because no one was addressing how to require/not require items, just handing out regexs: Regex working wrong, matching unexpected things From my post on that site, I've created a quick guide to assist anyone with making their own regex for their own desired phone number format, which I will caveat (like I did on the other site) that if you are too restrictive, you may not get the desired results, and there is no "one size fits all" solution to accepting all possible phone numbers in the world - only what you decide to accept as your format of choice. Note that it doesn't have any special rules for how many digits, or what numbers are valid in those digits, it just verifies that only digits, parenthesis, dashes, plus, space, pound, asterisk, period, comma, or the letters are present.

Take, for instance, my test script that downloads a web page and extracts all phone numbers using the regex. # my $external_filename = ' G-spring05.txt'; my @external_file = `curl $external_filename`; foreach my $line (@external_file) Edit: You can change \W* to \s*\W? I wasn't thinking of the regex in terms of, say, validating user input on a form when I wrote it, but this change makes it possible to use the regex for that purpose.

The international accounts for an optional initial ' ' and country code. Valid matches would be: My gut feeling is reinforced by the amount of replies to this topic - that there is a virtually infinite number of solutions to this problem, none of which are going to be elegant.

regular expression for validating phone number in javascript-15

who is ashanti dating right now

E.g (308)-135-7895 or 308-135-7895 or 308135-7895 or 3081357895.

A regular expression can be a single character, or a more complicated pattern.