Tell us what’s happening:
Please try to find what is wrong with the code? And, inbox me
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<div class="form">
<h1>Telephone Number Validator</h1>
<form class="form-container">
<input id="user-input" name="input" type="text">
<button id="check-btn" class="btn" >Check</button>
<button id="clear-btn" class="btn" >Clear</button>
</form>
<div id="results-div">
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</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 result=document.getElementById("results-div");
function isValid(telephone){
const pattern = /^1?\s*(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
if(pattern.test(telephone)){
return true
}
else{
return false;
}
}
checkBtn.addEventListener("click",(event)=>{
event.preventDefault()
if(userInput.value===""){
alert("Please provide a phone number");
}
else{
let item=document.createElement("p");
if(isValid(userInput.value)){
item.textContent=`Valid US number: ${userInput.value}`;
result.appendChild(item);
// result.textContent=`Valid US number: ${userInput.value}`;
}
else{
item.textContent=`Invalid US number: ${userInput.value}`;
result.appendChild(item);
// result.textContent=`Valid US number: ${userInput.value}`;
}
}
})
clearBtn.addEventListener("click",(event)=>{
event.preventDefault()
result.remove()
userInput.value="";
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator