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

Tell us what’s happening:

Hi! Cant seem to pass step 35, even though the code seems to work just fine, any ideas?

Your code so far

<!-- file: index.html -->
<input id="user-input" />
<button id="check-btn" >Check</button>
<button id="clear-btn" >Clear</button>
<div id="results-div" >
  Valid/Not valid shows here
</div>

<script src="script.js" >
</script>
/* file: script.js */
const userInput = document.getElementById("user-input");
const checkBtn = document.getElementById("check-btn");
const clearBtn = document.getElementById("clear-btn");
const resultsDiv = document.getElementById("results-div");

const validPhonePatterns = [
  /^1 \d{3}-\d{3}-\d{4}$/,    
  /^1 \(\d{3}\) \d{3}-\d{4}$/,
  /^1\(\d{3}\)\d{3}-\d{4}$/,
  /^1 \d{3} \d{3} \d{4}$/, 
  /^\d{10}$/,
  /^\d{3}-\d{3}-\d{4}$/,   
  /^\(\d{3}\)\d{3}-\d{4}$/,
];


function clearContent() {
  resultsDiv.innerText = "";
}

clearBtn.addEventListener("click", () => clearContent())


checkBtn.addEventListener("click", () => {
  const inputValue = userInput.value; 
  validNumber(inputValue);
  userInput.value = "";
  function validNumber(str) {
  if (str === "") {
    alert("Please provide a phone number");
    resultsDiv.innerText = "No bueno";
    return false;
  }
  const isValid = validPhonePatterns.some(pattern => pattern.test(str));
  if (isValid) {
    resultsDiv.innerText = `Valid US number: ${str}`;
  } else {
    resultsDiv.innerText = `Invalid US number: ${str}`;
  }
  return isValid;
}
});

/* file: styles.css */

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/18.3 Safari/605.1.15

Challenge Information:

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

I have added a console.log to see what is the output of your code for all tests

Valid US number: 1 555-555-5555
Valid US number: 1 (555) 555-5555
Valid US number: 5555555555
Valid US number: 555-555-5555
Valid US number: (555)555-5555
Valid US number: 1(555)555-5555
Invalid US number: 555-5555
Invalid US number: 5555555
Invalid US number: 1 555)555-5555
Valid US number: 1 555 555 5555
Valid US number: 1 456 789 4444
Invalid US number: 123**&!!asdf#
Invalid US number: 55555555
Invalid US number: (6054756961)
Invalid US number: 2 (757) 622-7382
Invalid US number: 0 (757) 622-7382
Invalid US number: -1 (757) 622-7382
Invalid US number: 2 757 622-7382
Invalid US number: 10 (757) 622-7382
Invalid US number: 27576227382
Invalid US number: (275)76227382
Invalid US number: 2(757)6227382
Invalid US number: 2(757)622-7382
Invalid US number: 555)-555-5555
Invalid US number: (555-555-5555
Invalid US number: (555)5(55?)-5555
Invalid US number: 55 55-55-555-5
Invalid US number: 11 555-555-5555
Valid US number: 1 853-764-8885
Invalid US number: 1 (832)677-4547
Invalid US number: 10 543-680-9924
Invalid US number: 1 (25)793-8096
Invalid US number: 1!(114)430-7532
Invalid US number: -1 085 430 8881
Invalid US number: 18692978
Invalid US number: 606#472-7311
Invalid US number: (591389-8428

For all these, I think this is wrong:

Invalid US number: 1 (832)677-4547
1 Like