Please, give me any ideas💡
The tests are failed in 5 sections, including “The number 1 555 555 5555 should be recognised as a valid one”. Why 1 456 789 4444 is valid and the above number is not valid?
The thing is that the “regex.test” method returns true and I have two switched result messages for this number: Valid and Invalid.
Also my “mismatched” function doesn’t work and I don’t know why, I still can see the numbers with mismatched parenthesis like the valid numbers…
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport", content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<link rel="stylesheet", href="styles.css">
</head>
<body>
<div class="form-container">
<label for="user-input">Enter a US phone number</label>
<input type="tel" id="user-input" placeholder ="Type your number here...">
<div id="button-container">
<button type="button" id="check-btn">Check</button>
<button type="button" id="clear-btn">Clear</button>
</div>
<div id="results-div"></div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
const userInput = document.getElementById("user-input");
const clearBtn = document.getElementById("clear-btn");
const checkBtn = document.getElementById("check-btn");
const result = document.getElementById("results-div");
const regex = /^1?\s?\(?[0-9]{3}\)?[-\s]?[0-9]{3}[-\s]?[0-9]{4}$/gi;
const mismatched = (depth, character) => {
depth = 0;
for (character in userInput) {
depth += character === '(';
depth -= character === ')';
if (depth < 0 ) {
break;
} else if (depth !== 0) {return false;}}
};
function checkInput () {
if (userInput.value === "") {
alert("Please provide a phone number");
} else if (regex.test(userInput.value) && mismatched) {
result.innerText = `Valid US number: ${userInput.value}`;
} else {
result.innerText = `Invalid US number: ${userInput.value}`;
}
}
function clearInput () {
result.innerText = "";
userInput.value = "";
}
checkBtn.addEventListener("click", checkInput);
clearBtn.addEventListener("click", clearInput);
/* file: styles.css */
body {
background-color:#ffe9e3;
color: #7c73e6;
font-size: 16;
width: 100vw;
height: 100vh;
}
label {
margin: 1em auto 1em auto;
padding: auto;
text-align: center;
text-shadow: 1px -1px #c4c1e0;
font-size: 2em;
display: block;
}
input, button {
background-color: #c4c1e0;
border: 1px solid #fafafa;
font-size: 1.1em;
border-radius: 5%;
font-family: times;
}
input {
display: block;
padding: auto;
width: 80%;
margin: 1em auto;
height: 15%;
font-size: 1.2em;
}
#button-container {
display: flex;
justify-content: space-evenly;
padding: auto;
width: 80%;
height: 10vh;
margin: 1em auto;
}
.form-container {
background-color: #fafafa;
width: 90vw;
height: 90vh;
margin: auto;
padding: 0;
}
button {
width: 40%;
height: 100%
}
#results-div {
background: #ffede3;
width: 70vw;
height: 50vw;
margin: auto;
padding: auto;
border-radius: 5%;
font-size: 1.2em;
}
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator