Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator

Tell us what’s happening:
Describe your issue in detail here.
Can someone explain what am I missing in the regex?

  **Your code so far**

function telephoneCheck(str) {
var regexNum = /[1\s]?([\d]{3})(\s)\1\2[\d]{4}|[1\s]?([\d]{3})(\-)\1\2[\d]{4}|[1\s]?[\d]{10}|[1\s]?\(([\d]{3})\)(\s)\1\-[\d]{4}|[1\s]?\(([\d]{3})\)\1\-[\d]{4}/;
return regexNum.test(str)
}

console.log(telephoneCheck("555-5555"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Telephone Number Validator

Link to the challenge:

OK, that is one long regexp :slight_smile: I would work on simplifying it just a bit. I think it is easier to start backwards on this one since you know that a phone number will always end with four digits. You basically have that already:

\-[\d]{4}/

You don’t need the square brackets here because the only thing you are looking for is digits, so you can simplify that to:

\-\d{4}/

Now is there always going to be a dash before the last four digits? Looking at the instructions the answer is no. There could also be a space, or there could be nothing (the last four digits immediately follow the preceding three digits). So you’ll want to change the \- into something that can take all of these scenarios into account.

Once you get this part working then move on to the three digits in front of the last four and apply the same logic. Then once you have that working you just need the area code, which is slightly more tricky since you have parens to be concerned about. Don’t forget that regular expressions have the equivalent of the OR operator to help you out.

Good luck!

Your approach really helped me but I’m stuck at this point:

function telephoneCheck(str) {

var regexNum = /-\d{4}[\s|-]?\d{3}[-|\s|)]?\d{3}[(|\s1]/;

return regexNum.test(str);

}

console.log(telephoneCheck(“555-555-5555”));

I cannot figure out what am I doing wrong

You’ve got this backwards. The last four digits should be at the end of the expression since they come at the end of the phone number, right :slight_smile: Notice in my examples above that I have them at the end:

\d{4}/

That forward slash is the end of the expression. So you’ll need to do a little rearranging first.

But I have added a negative sign before \d{4} like this -]d{4}. Can you please explain how to rearrange the regex?

I can’t think of simplifying it any further than this :

function telephoneCheck(str) {

  var regexNum = /[1\s]?\d{3}[\-\d{3}\-\d{4}|\s\d{3}\s\d{4}|\d{7}]|[1\s]?\(\d{3}\)\s?\d{3}\-\d{4}/;

  return regexNum.test(str);

}

I would really appreciate if you can go through the regex and tell me what’s wrong.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Let’s look at the end of your expression. This says to look for three numbers followed by a dash and then four numbers. So this will catch a phone number of the form 123-4567. But you know that valid phone numbers can also end with 123 4567 and 1234567. So you’ll want to fix this part of the expression so that it takes into account that there can be a space instead of a dash, or nothing between the numbers.

You can modify just the part of the expression pasted above to do this. It does not involve using the pipe | to OR things together. Let’s concentrate on just looking for either the dash or a space. Do you know of a way to use a regular expression to look for a single character that can be either one thing or another?

Lets suppose we are only using the dash (b/w the last three and four digits) instead of space so in that case we are bound to use either a dash , a bracket space or only a bracket between the next set of three digits like this :

555-555-5555
(555)555-5555
(555) 555-5555

Similarly, lets suppose we are using space (b/w the last three and four digits) instead of dash then we are bound to use only a space between the next set of three digits like this :

555 555 5555

That is why the regex is lengthy, and I can not incorporate a single character that can be one thing or another as you stated above.

With regular expressions you can do this very thing. Revisit this challenge:

Match Single Character with Multiple Possibilities

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