telephone number validator javascript

Solutions on MaxInterview for telephone number validator javascript by the best coders in the world

showing results for - "telephone number validator javascript"
Maya
07 Sep 2019
1// Valid US Phone Number but can be tweaked.
2
3const telephoneCheck = str => {
4  const regExp = /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm
5  return regExp.test(str)
6}
7
8telephoneCheck("27576227382");
9
10			/*			Regular Expression Explanation Below !!! 			*/
11
12/*
13	Expression : /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm
14    
15    ^ asserts position at start of a line
16    1? matches the character 1 literally (case sensitive)
17    ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
18    \s? matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
19    ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
20    1st Capturing Group (\(\d{3}\)|\d{3})
21    1st Alternative \(\d{3}\)
22    \( matches the character ( literally (case sensitive)
23    \d{3} matches a digit (equal to [0-9])
24    {3} Quantifier — Matches exactly 3 times
25    \) matches the character ) literally (case sensitive)
26    2nd Alternative \d{3}
27    \d{3} matches a digit (equal to [0-9])
28    {3} Quantifier — Matches exactly 3 times
29    2nd Capturing Group (\s|-)?
30    ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
31    1st Alternative \s
32    \s matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
33    2nd Alternative -
34    - matches the character - literally (case sensitive)
35    \d{3} matches a digit (equal to [0-9])
36    {3} Quantifier — Matches exactly 3 times
37    3rd Capturing Group (\s|-)?
38    ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
39    1st Alternative \s
40    \s matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
41    2nd Alternative -
42    \d{4} matches a digit (equal to [0-9])
43    {4} Quantifier — Matches exactly 4 times
44    $ asserts position at the end of a line
45    Global pattern flags
46    g modifier: global. All matches (don't return after first match)
47    m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
48*/
49
50// With love @kouqhar