Build a telephone number validator

// add event listener to wait until the html doc is fully loaded to run
document.addEventListener('DOMContentLoaded', () => {


// setting elements to variables
const userInput = document.getElementById ('user-input');
const checkButton = document.getElementById('check-btn');
const clearButton = document.getElementById('clear-btn');
const resultDisplay = document.getElementById('results-div');

 checkButton.addEventListener('click', () => {
   let input = userInput.value;

   if(input.trim() === '') {
     window.alert('Please provide a phone number');
    }else {
      // cleaned phone number down the just numbers without and space or syntax replaces regex items with an empty space
      const regex = /[\s\-\+\(\)\.]/g; 
      let cleanedNumber = input.replace(regex, ''); 

      if(cleanedNumber.length === 11 && cleanedNumber[0] === '1') {
        resultDisplay.textContent = `Valid US number: ${input}`;
      } else if (cleanedNumber.length === 10){
        resultDisplay.textContent = `Valid US number: ${input}`;
      } else {
        resultDisplay.textContent = `Invalid Us Number: ${input}`;
      }
    }
    
 });

//clear button functionality
clearButton.addEventListener('click', () => {
  userInput.value = '';
  resultDisplay.textContent = '';
})

});

<head>
  <title>NUMBER VERIFIER</title>
  <script defer src='script.js'></script>
  <meta charset="UTF-8">
  <linK rel='stylesheet' type='text/css' href='styles.css'>
</head>
<html lang='en'>
  <body>
    <input id='user-input' type="tel" name='phone'>
    <button id='check-btn'>Check</button>
    <button id='clear-btn'>Clear</button>
    <p id="results-div">Enter a result</p>


  </body>

</html>

this is my js and HTML im having issues with the number test cases that fail due to syntax format issues any help would be greatly appreciated

Can you post all your HTML and JS code please. Thanks

please share your html and a link to the project


I’ve edited your code 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 (').

1 Like

i just updated it my bad

thank you and my bad didnt know

you are removing parentheses and never checking if they are paired correctly

so it needs to be a string after running through the regex check? Also, I need a check before it hits the regex that checks if it’s in proper us phone number format.

it is a string there, you are not changing that with replace

parentheses and other symbols are part of what makes the number valid or not, but you’re not checking that

document.addEventListener('DOMContentLoaded', () => {


// setting elements to variables
const userInput = document.getElementById ('user-input');
const checkButton = document.getElementById('check-btn');
const clearButton = document.getElementById('clear-btn');
const resultDisplay = document.getElementById('results-div');


 checkButton.addEventListener('click', () => {
   let input = userInput.value;


   if(input.trim() === '') {
     window.alert('Please provide a phone number');
    } else {
      const regex =  /^(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;

     resultDisplay.textContent = regex.test(input) ? `Valid US number: ${input} ` : `Invalid US number: ${input}`;

    }
    
 });

//clear button functionality
clearButton.addEventListener('click', () => {
  userInput.value = '';
  resultDisplay.textContent = '';
})







});```

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Yes, it’s an invaluable aspect of communication let alone learning to program at any level, but unfortunately, people have lives and can’t answer promptly. I’ve figured out my issues and solved the project with guidance from GPT architecture. Using the guideline of not generating a single line of code and only revising the logistical approach and explaining methods (.trim, ? operator, .test, etc…). Here’s the finished project.

document.addEventListener('DOMContentLoaded', () => {
  // setting elements to variables
  const userInput = document.getElementById('user-input');
  const checkButton = document.getElementById('check-btn');
  const clearButton = document.getElementById('clear-btn');
  const resultDisplay = document.getElementById('results-div');

  checkButton.addEventListener('click', () => {
    let input = userInput.value;

    // Check if input is empty
    if (input.trim() === '') {
      window.alert('Please provide a phone number');
    } else {
      // Updated regex to match valid US phone numbers
      const regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s.-]?\d{3}[\s.-]?\d{4}$/;

      // Update the result display based on whether the input matches the regex
      resultDisplay.textContent = regex.test(input)
        ? `Valid US number: ${input}`
        : `Invalid US number: ${input}`;
    }
  });

  // Clear button functionality
  clearButton.addEventListener('click', () => {
    userInput.value = '';
    resultDisplay.textContent = '';
  });
});

The issue was due to a lack of awareness of how in-depth and specific regex values can be. Thanks to GPT there was a logistical revision along with a realization there was no need to remove any whitespace or syntax outside of using .trim for whitespace at the beginning or end of the string. The regex should be formatted in such a manner that phone numbers in the provided example would pass through the regex.test().
1 555-555-5555
1 (555) 555-5555
1(555)555-5555
1 555 555 5555
5555555555
555-555-5555
(555)555-5555

Thank you for your time also, I do apologize for the lack of clarity on the situation.

Granted I will admittingly say AI does still have flaws and It makes it easier for me personally but can lead others to an issue of actually having to read and understand what it’s producing/approach it’s taking toward a solution. More frequently there are errors in generated/user logic or a lack of in-depth explanation provided by the user. It also seems to try to be more pleasing rather than corrective.