const resultsDiv = document.getElementById('results');
const value = '2555-555-5555';
const regex = /(1s?)?(d{3}|(d{3}))[-s]?d{3}[-s]?d{4}/; //I found this is an incorrect regex
if (regex.test(value)) {
console.log(regex.test(value)); //is false
resultsDiv.querySelector("p").textContent = `Valid US number: ${value}`;
} else {
resultsDiv.querySelector("p").textContent = `Invalid US number: ${value}`;
}
<div id="results"><p></p></div>
The if condition here should not be executing.
2
Answers
i have looked to your example, modify some things statically and the result is fine
the result is :
else
false
Can you try like this
Valid
Console: [‘(555) 555-1234’]
Valid US number: (555) 555-1234
Invalid Phone
Invalid US number: 99999
Use match https://www.w3schools.com/jsref/jsref_match.asp