JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Tell us what’s happening:
I’ll need some help with this, and I’m trying to clear the three tests,
telephoneCheck("1 555)555-5555") should return false .
telephoneCheck("555)-555-5555") should return false.
telephoneCheck("(555-555-5555") should return false.”

Can anyone assist me with this?

Your code so far

function telephoneCheck(str) {
let telRegex = /^1(\s)?\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$|^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

return telRegex.test(str);
}

telephoneCheck("555-555-5555");

Your browser information:

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

Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Link to the challenge:

Consider that the \(?and \)? around the first part of digits will accept patterns with - no parenthesis, single parenthesis (on one of the sides) and two parenthesis (on both sides).

1 Like

And the first pair of digits are the “^” and the “1”? Or is it something else?

This part to be exact \(?(\d{3})\)?

So, do I have to change it or something? I know you can’t give me any hints on this, I’m just trying to figure it out.

Well, yeah. That’s the reason why those test are getting incorrect results, so something needs to be changed.

What in that part of code needs to be change?

Hello? Are you going to reply?

what kind of reply are you looking for? (looks like the last thing you asked was:

and the response someone gave you was:

not sure why you are messaging that person for something?

I was asking “What in that part of code needs to be change?” I know you won’t probably give me any hints due to being a moderator and all.

one tool that I would suggest you try is this one

You put your regex into it and the strings that you need it to work on
then modify it to try to get closer to the answer

Right now I think your pattern looks complex so maybe I would look at trying to simplify it

1 Like

I’m not sure how to work this website.

Just put your pattern into the pattern field (this part here)

And then put some test strings in the box below

The pattern will either match or not match

And you can examine the specific parts of the pattern to remind yourself what each part does

I’m not exactly sure how to get it to work…

The tool?
I just put your pattern in the top field (the regular expression field)
And then I pasted the test strings in the box below.
And for eg I found the area of the pattern that was matching the ) for eg. Then from there you have to decide what to do. For eg maybe you want to have a conditional pattern to match the left ( bracket first and if found match the right.)
The tool is there to help but you don’t have to use it.

And would these be good for test strings?

telephoneCheck(“1 555)555-5555”)
telephoneCheck(“555)-555-5555”)
telephoneCheck(“(555-555-5555”)

No just the actual string

555-555-5555
1-555-555-5555

Etc

Oh, ok. Thank you for the info.

What about…

1 555)555-5555
555)-555-5555

and

(555-555-5555

?

yes sure, you can add any strings
just make sure you remember which ones are ‘bad’ and which ones are ‘good’
(you don’t want to match the bad ones)