-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
37 lines (31 loc) · 1.24 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const userInput = document.getElementById("user-input");
const checkBtn = document.getElementById("check-btn");
const clearBtn = document.getElementById("clear-btn");
const results = document.getElementById("results-div");
// Validates number input based on regular expression
const validateNumber = num => {
if (num.length < 10) { return false; }
// Eliminate whitespace from number and check validity on digits and special characters (i.e. dashes & parentheses)
const trimmedNum = num.replace(/\s/g, "");
const numberRegex = /^1?(\(\d{3}\)|\d{3})?-?\d{3}-?\d{4}$/;
return trimmedNum.match(numberRegex);
}
// Event listener for check button
checkBtn.addEventListener("click", e => {
e.preventDefault();
// Checks if user filled something in before validating
if (!userInput.value) { alert("Please provide a phone number"); }
else {
if (validateNumber(userInput.value)) {
results.textContent = `Valid US number: ${userInput.value}`;
} else {
results.textContent = `Invalid US number: ${userInput.value}`;
}
}
userInput.value = "";
})
// Clears input field upon button press
clearBtn.addEventListener("click", e => {
e.preventDefault();
results.textContent = "";
})