Tell us what’s happening:
The validator appears to fulfill all tests,yet it does not pass.What is wrong?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<head>
<html lang="en">
<meta charset="UTF-8"><meta name="viewport"
content=width="device-width,initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
TELEPHONE NUMBER VALIDATION
<input type="text"id="user-input">
<button id="check-btn">CHECK</button>
<button id="clear-btn">CLEAR</button>
<p id="results-div"></p>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
const checkBtn= document.getElementById("check-btn");
const clearBtn=document.getElementById("clear-btn");
const input=document.getElementById("user-input");
const resultsDiv=document.getElementById("results-div")
function hasMisplacedParenthesis(rawValue){return /\)[^\d]*\d/.test(rawValue) && !/\(\d{3}\)/.test(rawValue);}
function validity(rawValue){const allSame=/^(\d)\1+$/;
const digitsOnly=rawValue.replace(/\D/g,'')
if(allSame.test(digitsOnly)&&digitsOnly.length===10 &&rogueCheck(rawValue)||rawValue.length>=11&&rawValue.length<=13&&allSame.test(digitsOnly) && rogueCheck(rawValue)){return true;}
if(digitsOnly.length===10&&!allSame.test(digitsOnly)&&rogueCheck(rawValue)||rawValue.length>=11&&rawValue.length<=13)
{return true;
}
if(!rogueCheck(rawValue)){return false;}
if ((digitsOnly.length===11 && digitsOnly.startsWith("1")&&rogueCheck(rawValue)||(rawValue.length>=12&&rawValue.length<=14 &&digitsOnly.startsWith("1")&&rogueCheck(rawValue))))
{return true;
}
else{return false;}
}
function rogueCheck(rawValue){const pattern=/^(\+?\d{1,2}\s?)?(\(\d{3}\)|\d{3})[\s-]?\d{3}[\s-]?\d{4}$/;
return pattern.test(rawValue)
}
function check(){
const rawValue=input.value.trim();
if(!rawValue){alert("Please provide a phone number");
return;}
if(validity(rawValue)&&!hasMisplacedParenthesis(rawValue))
{resultsDiv.textContent=`"Valid US number: ${input.value}"`;
}
else resultsDiv.textContent=`"Invalid US number: ${input.value}"`;
}
function clear(){resultsDiv.textContent="";
input.value="";
}
checkBtn.addEventListener("click",check);
clearBtn.addEventListener("click",clear);
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator