Telephone Number Validator initial thoughts before tomorrow's class

So, I have decided to leave the OOP homework for another day, as I am confused and have a very sore head! :sweat_smile:

I made an initial attempt at this - I know it is wrong on many levels, but I’m posting it to save it before tomorrow’s class. I feel this should be straight forward, applying the regex knowledge picked up on the course, but I can’t quite get the structure right. And the more I try, the worse it gets. :smile: So I’m stopping for today so I have time for lunch before my actual paid work starts.

Most of this is actually commented out - but it is super hard to read - on my browser the commented out sections are in green… sorry bout that.

> function telephoneCheck(str) {
>   //let telephoneNum = str;
>   // write regex to cover 1 alone at the beginning ^1 but no other number alone - you also use [^2-9] ^ within the [] to mean "not"
>   // also need to eliminate numbers less than zero
>   // how to add the space requirement \s?
>   // with or || covering any set of three or more letters at the beginning
>   // set requirement that no letters allowed
>   // add requirement that if brackets are present, they only surround the first group of three numbers
>   // add an or requirement to ignore a 1 and start counting the first group of three numbers after a 1 if a 1 is present
>   // why does [^A-Za-z] not eliminate matches with letters???
>   let numRegex = /^[^1\s][^2-9]\s\d+[^A-Za-z][\d\d\d\d$]|| ^\d+\d\d\[^A-Za-z][^\D][\d\d\d\d$]/g;
>   //let numRegex = /[^\D]/;
>   //let telephoneCheck = /^[^1\s][[^2-9]\s]\d+[^\D]|| ^\d+\d\d\[^\D]/g;
>   //let americanNum = telephoneNum.match(numRegex); 
>   numRegex.test(str);
>  // console.log(numRegex.test(str));
>   //let americanNum = str.match(numRegex); 
>   //if (americanNum = str.match(numRegex)) {
> // why does console.log(str.match(numRegex));
> // return this??? [ '', '', '', '', '', '', '', '', '', '', '', '', '' ]
>   return true;
>   //let result = telephoneCheck.test(str);
> }
> // why can i not get anything to return as false - because i am returning an array made of empty spaces!?
> console.log(telephoneCheck("555-555-5555"));
1 Like

Hi @Jane2963637

This is not an easy regex. Well done! First step it is always understand the problem and then try something and rethink it.

I had to console.log() all TRUE and FALSE tests at the end of my code to follow my thoughts correctly. And make it work by chunks, first 1 or not, then 555 or (555) and so on…

At the same time I have documented the regex chunks to not get lose in their weirdness.

You can check mdn regex cheatsheet as you go too, it helped me.

I meant something like this:

function telephoneCheck(str) {

 return str.match(regex);  // to visualize the result first, chunk by chunk

  // return regex.test(str);  This can be the return when you finish your tests

// tests here commented, and I have uncommented one by one as I was writing regexs chunks.

I forgot something: this a great resource regex101 to check your regex, note that it has flags on and you are writing inside the regex. Look the right side of the web after write your regex. ;D

Happy coding!


Thanks Carlos, I will look into this a little later, looks very useful indeed. :smiling_face:


I just remembered i solved this telephone number validator last week. if you can make use of the resource that @carlost2672543 shared, regex101, it is really helpful.

I will post my code tomorrow.


Tripple that for

One important thing to add to the other comments: in the lower left corner of this site you’ll find a frequently overlooked ‘Regex Debugger’.


This allows you to step through your regex and see what happens, much like the code visualizer on JavaScript coding tutor - Learn JavaScript by visualizing code execution but then for regex. I hope you find that useful.


Ooh, lovely, thanks Syl for this and for the debugger! I will try this later tonight.

1 Like

Started again, checked for the first character and some other patterns, much cleaner code this time.
Update: much busier regex but it works for 24/29 tests. A definite improvement on before.

The only ones I am failing on now are ones with unmatched brackets (e.g. a closing bracket with no opening bracket) and a string with irregular grouping of the numbers. I look forward to seeing the proper (for want of a better term) code tomorrow. I know mine contains too many | or clauses and, because of this, far too many repetitions. But it works for the things I am checking for.

Thanks to Carlos and Syl for the great help! You really helped me streamline my thinking. I wish I could help people back, maybe one day. :grin:

function telephoneCheck(str) {

let telephoneNum = str;
 let numRegex = /(?=.{10,})^1[555]|(?=.{10,})^1\ [555]|(?=.{10,})^1\ \(|(?=.{10,})^1\(|(?=.{10,})^1\ \d|(?=.{10,})^5[55]|(?=.{10,})^\([555]/g;
 return numRegex.test(telephoneNum);



This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.