-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (41 loc) · 1.27 KB
/
script.js
File metadata and controls
46 lines (41 loc) · 1.27 KB
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
38
39
40
41
42
43
44
45
46
const userInput = document.getElementById('user-input');
const checkBtn = document.getElementById('check-btn');
const clearBtn = document.getElementById('clear-btn');
const resultsDiv = document.getElementById('results-div');
const checkValidNumber = input => {
if (input === '') {
alert('Please provide a phone number');
return;
}
const countryCode = '^(1\\s?)?';
const areaCode = '(\\([0-9]{3}\\)|[0-9]{3})';
const spacesDashes = '[\\s\\-]?';
const phoneNumber = '[0-9]{3}[\\s\\-]?[0-9]{4}$';
const phoneRegex = new RegExp(
`${countryCode}${areaCode}${spacesDashes}${phoneNumber}`
);
const pTag = document.createElement('p');
pTag.className = 'results-text';
phoneRegex.test(input)
? (pTag.style.color = '#00471b')
: (pTag.style.color = '#4d3800');
pTag.appendChild(
document.createTextNode(
`${phoneRegex.test(input) ? 'Valid' : 'Invalid'} US number: ${input}`
)
);
resultsDiv.appendChild(pTag);
};
checkBtn.addEventListener('click', () => {
checkValidNumber(userInput.value);
userInput.value = '';
});
userInput.addEventListener('keydown', e => {
if (e.key === 'Enter') {
checkValidNumber(userInput.value);
userInput.value = '';
}
});
clearBtn.addEventListener('click', () => {
resultsDiv.textContent = '';
});