Using a regular expression, you need to return true if the number matches, otherwise false .
Wrote such code

 //Logic for testing function isTrue(str) { if(telephoneCheck(str)) { return '<span style="color:#00ff00">OK - telephoneCheck("'+str+'") return true</span><br>'; } else { return '<span style="color:#ff0000">ERROR - telephoneCheck("'+str+'") return false (MUST BE true)</span><br>'; } } function isFalse(str) { if(telephoneCheck(str)) { return '<span style="color:#ff0000">ERROR - telephoneCheck("'+str+'") return true (MUST BE false)</span><br>'; } else { return '<span style="color:#00ff00">OK - telephoneCheck("'+str+'") return false</span><br>'; } } function TEST() { var retCode=''; retCode+=isTrue("555-555-5555"); retCode+=isTrue("1 555-555-5555"); retCode+=isTrue("1 (555) 555-5555"); retCode+=isTrue("5555555555"); retCode+=isTrue("555-555-5555"); retCode+=isTrue("(555)555-5555"); retCode+=isTrue("1(555)555-5555"); retCode+=isTrue("1 555 555 5555"); retCode+=isTrue("1 456 789 4444"); retCode+=isFalse("123**&!!asdf#"); retCode+=isFalse("55555555"); retCode+=isFalse("(6505552368)"); retCode+=isFalse("2 (757) 622-7382"); retCode+=isFalse("0 (757) 622-7382"); retCode+=isFalse("-1 (757) 622-7382"); retCode+=isFalse("2 757 622-7382"); retCode+=isFalse("10 (757) 622-7382"); retCode+=isFalse("27576227382"); retCode+=isFalse("(275)76227382"); retCode+=isFalse("2(757)6227382"); retCode+=isFalse("2(757)622-7382"); retCode+=isFalse("555)-555-5555"); retCode+=isFalse("(555-555-5555"); retCode+=isFalse("(555)5(55?)-5555"); retCode+=isFalse("555-555"); retCode+=isFalse("5555555"); retCode+=isFalse("1 555)555-5555"); document.getElementById('content').innerHTML = retCode; } 
 <!DOCTYPE html> <html> <head> </head> <body> <script> function telephoneCheck(str) {//===!!!FixIt!!!=== var regex = /1?\s?[\s\(]?[0-9]{3}[\)\s\-]?\s?[0-9]{3}[\s\-]*[0-9]{4}[^0-9]/g; if (regex.test(str)) { return true; } return false; } </script> <input type=button value="TEST" onclick="TEST()"/> <div id='content'> </div> </body> </html> 

The same but on Plunker

Not sure if the regular schedule is appropriate and looks confusing and not sure if you need to use test , maybe search or match .
In response, it is desirable to explain your regular season.

The fix function is marked with a FixIt comment.

The list of regular numbers used in the test

Good ones
"555-555-5555"
"1 555-555-5555"
"1 (555) 555-5555"
"5555555555"
"555-555-5555"
"(555) 555-5555"
"1 (555) 555-5555"
"1 555 555 5555"
"1 456 789 4444"

The bad
"123 ** & !! asdf #"
"55555555"
"(6505552368)"
"2 (757) 622-7382"
"0 (757) 622-7382"
"-1 (757) 622-7382"
"2 757 622-7382"
"10 (757) 622-7382"
"27576227382"
"(275) 76227382"
"2 (757) 6227382"
"2 (757) 622-7382"
"555) -555-5555"
"(555-555-5555"
"(555) 5 (55?) - 5555"
"555-555"
"5555555"
"1 555) 555-5555"

Closed due to the fact that off-topic participants Kromster , Bald , HamSter , aleksandr barakin , cheops 6 Oct '16 at 17:46 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Bald, HamSter, aleksandr barakin, cheops
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Well ocheshuet :) Forgot the price tag set. Or at least examples of valid numbers. - user207618
  • RegExp#test() is what you need, only g should be removed. Well, fix the regular season. - Wiktor Stribiżew
  • Examples in the demo, right above the eye to click. After changing the code, press the green button with the arrows inside, the example is updated and will check the passing of the tests. - stackanon
  • It is necessary to search out the tests for what you need to check? - user207618
  • 2
    In response, it is desirable to explain your regular season. - In the question, it is desirable to explain the verification criteria, as well as the list of "good" and separately "bad" lines. That's when things will get off the ground. - Wiktor Stribiżew

1 answer 1

 var regex = /^(1[ ]?)?(((\([0-9]{3}\)[ ]?)|([0-9]{3}[- ]?)))[0-9]{3}[- ]?[0-9]{4}$/; 

And it works like this:

 ^ начало строки ( 1[ ]? 1 с пробелом или без )? есть или нет конструкции выше ( ( ( \([0-9]{3}\) з цифры в скобках [ ]? с пробелом или без ) | или ( [0-9]{3} з цифры без скобок [- ]? с дефисом, пробелом или без них ) ) ) [0-9]{3} з цифры [- ]? с дефисом, пробелом или без них [0-9]{4} 4 цифры $ конец строки 
  • The code may not be laconic enough and not the coolest. But it works. - Ilya
  • I also want to clarify: when checking 1 555-5555555 - true when checking 1 (555) -555-5555 - false These examples are not in your condition. - Ilya