Build a Telephone Number Validator Project - Build a Telephone Number Validator

Tell us what’s happening: I can’t pass one only test.

Describe your issue in detail here.
I’ve one only problem, and with the following test:
When the #user-input element contains 1 456 789 4444 and the #check-btn element is clicked, the #results-div element should contain the text Valid US number: 1 456 789 4444 .

If I run the test on my personal IDE all works fine, on the other hand I’m sure that the following regex matches perfectly the test (const regex4 = /^[1][\s][\d]{3}[\s][\d]{3}[\s][\d]{4}$/g). It’s the unique test that I haven’t completed, it’s so strange.
What’s going on here? Thx for reply

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="certification_telNumber" />
    <title>certification no.3</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  
  <body>
    <h1>US Phone Number Validator</h1>
    <div class="input-container">
      <label for="user-input">Phone:</label>
      <input
        id="user-input"
      />
      <button id="check-btn">Check</button>
      <button id="clear-btn">Clear</button>
    </div>
    <div id = 'results-div'>
    <output for="results-div"></output>
    </div>
  </body>
  <script src="script.js"></script>
</html>
/* file: styles.css */
body {
    width: 100%;
    height: 100%;
    min-width: 400px;
    margin: 0;
    background-color: blanchedalmond;
    font-family: 'Menlo', Times, serif;
    font-size: 16px;
}

h1,p{
    margin: 1em auto;
    text-align: center;
}

.input-container{
    text-align: center;
}
#results-div{
    margin: 2rem 12rem 2rem 12rem;
    min-width: 6rem;
    padding: 1rem;
    text-align: center;
    border-style: inset;
    background-color: azure;
}
/* file: script.js */
const input = document.getElementById('user-input');
const checkBtn = document.getElementById('check-btn');
const clearBtn = document.getElementById('clear-btn');
const textResult = document.getElementById('results-div');

const regex1 = /^[1][\s][\d]{3}[-][\d]{3}[-][\d]{4}$/g;
const regex2 = /^[1][\s][\(][\d]{3}[\)][\s][\d]{3}[-][\d]{4}$/g; 
const regex3 = /^[1][\(][\d]{3}[\)][\d]{3}[-][\d]{4}$/g;
const regex4 = /^[1][\s][\d]{3}[\s][\d]{3}[\s][\d]{4}$/g
const regex5 = /^[\d]{10}$/g;
const regex6 = /^[\d]{3}[-][\d]{3}[-][\d]{4}$/g
const regex7 = /^[\(][\d]{3}[\)][\d]{3}[-][\d]{4}$/g

const arrayRegex = [regex1,regex2,regex3,regex4,regex5,regex6,regex7];

const evalPhoneNum = input => arrayRegex.some(regex => regex.test(input));

clearBtn.addEventListener('click', () => 
    (input.value = '')(textResult.textContent = ''));
checkBtn.addEventListener('click', () => {
    console.log(`clicked! input: ${input.value}`);
    if (input.value === ''){
        alert('Please provide a phone number'); 
        return;
    } 
    evalPhoneNum(input.value)?
        textResult.textContent = `Valid US number: ${input.value}` :
        textResult.textContent = `Invalid US number: ${input.value}`;
});


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15

Challenge Information:

Build a Telephone Number Validator Project - Build a Telephone Number Validator

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Hi @NicolaNDP
image

If the number is valid without round brackets, should it be valid with round brackets?

Happy coding

I’m not sure why you decided to have multiple regex patterns to check. You can use the optional operator ( ? ) which will optionally match the preceding str or group. Thus this exercise can be simplified to one regex pattern to match with regex.test(input)

Example:
let regex = /1?\d{3}/g

This will match a pattern that has either
A: 3 numerical digits in a row
B: the number 1 followed by 3 digits

Here’s my full Regex Notes:

Regex - way to search through string of text for validation or find / replace methods

Syntax: Example:

/expression/flags Let regExp = /the/g

Special Regex Characters
“+” char - match 1 or more of preceding str value

“?” char - optional match of preceding str or group

“*” char - match zero or more in a row

“.” char will match anything except a new line

“\” char - escape and treat following str value as normal value - /./g

“\w” char - match any “word” char

“\W” char - match any non “word char

“\s” char - match any space

“\S” char - match any non-space

“\d” char - match any digit 0-9

Regex Operators

“|” char - the OR operator in regex (typically used with parenthesis)

“^” char - carrot symbol allows you to match from the beginning of the line

“$” char - dollar symbol allow symbol to match from the end of the line

“(?<=)” - positive look behind

-ex: /(?<=[tT]he)./g will match the next character after the or The

“(?<!)” - negative look behind

-ex: /(?<![T]he)./g will match anything that DOESN’T have The before it

“(?=) - positive look ahead

-ex: /.(?=at)/g will find any char with “at” after it (i.e. “c” in cat)

“(?!) - negative look ahead

-ex: /.(?!at)/g will find ALL chars WITHOUT “at” after it

Regex Groups

“{ }” - curly braces allows to set min / max size (if 1 num given, min is default)

Syntax: {min, max} Example: /w{4,5}\g - match any 4 or 5 char word

” - brackets allow you to match a set

Example: /[a-zA-Z]/g will match all alphabet chars

Example: /[0-9]/g will match all numbers

“( )” - capture grouping - parentheses act as their own “group”

Hi @Teller, thanks for reply!
No, the test reports specific cases to valid, invalid or who knows. The specific form of the number that you are asking for is not in the test, not for the valid numbers, not for the invalid numbers.

Happy coding to you!

Hi @james.wismer24 and thanks for your reply!

As I was telling before, I have 7 cases of pattern to validate and a lot of pattern to invalidate. Solutions that uses a single regex pattern may be the right for matching all the pattern to validate, but with a big chance to matching some pattern to invalidate. I’ve tried to solve with a single regex, but the cases to invalidate are a lot. So I’ve taken the ‘fast’ solution: one pattern per valid cases.

I think it is possible to find a better solution, using less pattern, combining characteristics of 2,3,4 of them in a single combined pattern, but for now I’m good with that.

Thanks a loooot for your Regex notes!
Happy coding!