-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (67 loc) · 2.64 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const nameFirst = document.getElementById('name_first')
const nameLast = document.getElementById('name_last')
const emailAddress = document.getElementById('email_address')
const password = document.getElementById('password')
const errorNameFirst = document.getElementById('error_name_first')
const errorNameLast = document.getElementById('error_name_last')
const errorEmail = document.getElementById('error_email')
const errorPassword = document.getElementById('error_password')
const submit = document.getElementById('form_btn')
// set error msgs to hidden on page load
errorNameFirst.classList.add('hidden')
errorNameLast.classList.add('hidden')
errorEmail.classList.add('hidden')
errorPassword.classList.add('hidden')
// confirm page loads with empty input fields
nameFirst.value = ''
nameLast.value = ''
emailAddress.value = ''
password.value = ''
//check each input, if invalid then remove hidden from error msgs,
//change text to red, and change border to red
const formValidation = (e) => {
e.preventDefault()
if (nameFirst.value == '') {
errorNameFirst.classList.remove('hidden')
nameFirst.style.border = '1px solid var(--Red)'
nameFirst.style.color = 'var(--Red)'
}
if (nameLast.value == '') {
errorNameLast.classList.remove('hidden')
nameLast.style.border = '1px solid var(--Red)'
nameLast.style.color = 'var(--Red)'
}
if (password.value == '') {
errorPassword.classList.remove('hidden')
password.style.border = '1px solid var(--Red)'
password.style.color = 'var(--Red)'
}
if (!emailAddress.value.match(/^[A-Za-z0-9]+@[A-Za-z0-9]+\.[A-Za-z]{2,}$/)) {
errorEmail.classList.remove('hidden')
emailAddress.style.border = '1px solid var(--Red)'
emailAddress.style.color = 'var(--Red)'
}
return
}
// run validator function on button click
submit.addEventListener('click', formValidation)
nameFirst.addEventListener('focus', function() {
this.style.border = '1px solid #DEDEDE'
this.style.color = 'var(--Dark-Blue)'
errorNameFirst.classList.add('hidden')
})
nameLast.addEventListener('focus', function() {
this.style.border = '1px solid #DEDEDE'
this.style.color = 'var(--Dark-Blue)'
errorNameLast.classList.add('hidden')
})
emailAddress.addEventListener('focus', function() {
this.style.border = '1px solid #DEDEDE'
this.style.color = 'var(--Dark-Blue)'
errorEmail.classList.add('hidden')
})
password.addEventListener('focus', function() {
this.style.border = '1px solid #DEDEDE'
this.style.color = 'var(--Dark-Blue)'
errorPassword.classList.add('hidden')
})