-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
182 lines (149 loc) · 5.36 KB
/
Copy pathscript.js
File metadata and controls
182 lines (149 loc) · 5.36 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const form = document.getElementsByTagName('form')[0]
const email = document.querySelector('#email');
const emailError = document.querySelector('#emailError');
let emailValid = false;
const validateEmail = ()=>{
email.addEventListener('keyup', ()=>{
if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){ //checks if valid email format
emailValid = true;
emailError.textContent = 'Valid e-mail entered.'
emailError.classList.remove('error');
emailError.classList.add('valid');
} else{
emailError.textContent = '*Please enter a valid e-mail address';
}
})
email.addEventListener('blur', ()=>{
emailError.style.display = 'none'; //removes message when clicked outside
})
}
validateEmail();
let pInput = document.querySelector('#password');
const numbers = /[0-9]/g;
const upperCaseLetters = /[A-Z]/g;
const msg = document.querySelector('#message')
const upper = document.querySelector('#pUppercase');
const number = document.querySelector('#pNumber');
const length = document.querySelector('#pLength');
let passwordValid = false;
const validatePassword = ()=>{
password.addEventListener('keyup', ()=>{
if(password.value.match(upperCaseLetters)){ //checks if uppercase letter exists
upper.classList.remove('error');
upper.classList.add('valid');
passwordValid = true;
}
else {
upper.classList.remove('valid');
upper.classList.add('error');
passwordValid = false;
}
if(password.value.match(numbers)){ //checks if a number exists
number.classList.remove('error');
number.classList.add('valid');
passwordValid = true;
}
else {
number.classList.remove('valid');
number.classList.add('error');
passwordValid = false;
}
if(password.value.length >= 9) {
length.classList.remove('error'); //checks password length
length.classList.add('valid');
passwordValid = true;
}
else {
length.classList.remove('valid');
length.classList.add('error');
passwordValid = false;
}
})
if(password.value.length == 0){
passwordValid = false;
}
password.addEventListener('focus', ()=>{
msg.style.display = 'block'; //display message when focused
})
password.addEventListener('blur', ()=>{
msg.style.display = 'none'; //removes message when clicked outside
})
}
validatePassword();
const confirmPassword = document.querySelector('#confirmPassword');
const confirmError = document.querySelector('#confirmError');
let passwordsMatch = false;
const confirmMatch = ()=>{
confirmPassword.addEventListener('keyup', ()=>{
confirmError.style.display = 'block';
if(confirmPassword.value == password.value) {
confirmError.textContent = 'Passwords match';
confirmError.classList.remove('error');
confirmError.classList.add('valid');
passwordsMatch = true;
}
else{
confirmError.textContent ='Passwords do not match'
confirmError.classList.remove('valid');
confirmError.classList.add('error');
passwordsMatch = false;
}
})
confirmPassword.addEventListener('blur', ()=>{
confirmError.style.display = 'none'; //removes message when clicked outside
})
}
confirmMatch();
const country = document.querySelector('#country')
const zipcode = document.querySelector('#zipcode')
const zipMsg = document.querySelector('#zipMsg');
let zipValid = false;
const validateZip = ()=>{
zipcode.addEventListener('keyup', ()=>{
zipMsg.style.display = 'block';
if(zipcode.value.length == 5 ) {
zipMsg.classList.remove('error');
zipMsg.classList.add('valid');
zipValid = true;
}
else{
zipMsg.classList.remove('valid');
zipMsg.classList.add('error');
zipValid = false;
}
})
zipcode.addEventListener('blur', ()=>{
zipMsg.style.display = 'none'; //removes message when clicked outside
})
}
validateZip();
const validateForm = ()=>{
form.addEventListener('submit', (e)=>{
if(emailValid === false) {
email.style.border = '2px solid red';
email.style.background = '#f3d7d7'
e.preventDefault();
}
if(passwordValid === false) {
password.style.border = '2px solid red';
password.style.background = '#f3d7d7'
e.preventDefault();
}
if(passwordsMatch === false) {
confirmPassword.style.border = '2px solid red';
confirmPassword.style.background = '#f3d7d7'
e.preventDefault();
}
if(country.value == 'Select') {
country.style.border = '2px solid red';
country.style.background = '#f3d7d7';
e.preventDefault();
}
if(zipValid === false) {
zipcode.style.border = '2px solid red';
zipcode.style.background = '#f3d7d7'
e.preventDefault();
}
})
}
validateForm();