Tell us what’s happening:
why does the commented out code allow the number 555-555–5555 (two hyphens)
but the new code says this is false and only allows one space or hyphen (which is what i want)
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* 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 checkValidNumber = input => {
if (input === '') {
alert('Please enter a number')
}
// const countryCode = '^(1\\s?)?';
// const areaCode = '(\\([0-9]{3}\\))|([0-9]{3})[\\s\\-]?';
// const phoneNumber = '[0-9]{3}[\\s\\-]?[0-9]{4}$';
// const regex = new RegExp(
// `${countryCode}${areaCode}${phoneNumber}`
// )
const countryCode = '^(1\\s?)?';
const areaCode = '(\\([0-9]{3}\\)|[0-9]{3})';
const spacesDashes = '[\\s\\-]?';
const phoneNumber = '[0-9]{3}[\\s\\-]?[0-9]{4}$';
const phoneRegex = new RegExp(
`${countryCode}${areaCode}${spacesDashes}${phoneNumber}`
);
console.log(phoneRegex.test(input));
}
checkBtn.addEventListener('click', () => {
checkValidNumber(userInput.value);
})
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator


