-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (41 loc) · 1.34 KB
/
app.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
38
39
40
41
42
43
44
45
46
47
48
49
// Form Blur Event Listeners
document.getElementById('name').addEventListener('blur', validateName);
document.getElementById('zip').addEventListener('blur', validateZip);
document.getElementById('email').addEventListener('blur', validateEmail);
document.getElementById('phone').addEventListener('blur', validatePhone);
function validateName() {
const name = document.getElementById('name');
const re = /^[a-zA-Z]{2,10}$/;
if (!re.test(name.value)) {
name.classList.add('is-invalid');
} else {
name.classList.remove('is-invalid');
}
}
function validateZip() {
const zip = document.getElementById('zip');
const re = /^[0-9]{5}(-[0-9]{4})?$/; //00005(-0004) where -0004 is optional
if (!re.test(zip.value)) {
zip.classList.add('is-invalid');
} else {
zip.classList.remove('is-invalid');
}
}
function validateEmail() {
const email = document.getElementById('email');
const re = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
if (!re.test(email.value)) {
email.classList.add('is-invalid');
} else {
email.classList.remove('is-invalid');
}
}
function validatePhone() {
const phone = document.getElementById('phone');
const re = /^\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$/;
if (!re.test(phone.value)) {
phone.classList.add('is-invalid');
} else {
phone.classList.remove('is-invalid');
}
}