-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (42 loc) · 1.37 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
50
// 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);
// Functions
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})?$/;
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 = /^([0-9]{3,7}+)\-([0-9]{8,11})$/;
if(!re.test(phone.value)) {
phone.classList.add('is-invalid');
}else {
phone.classList.remove('is-invalid');
}
}