-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (71 loc) · 2.56 KB
/
index.js
File metadata and controls
78 lines (71 loc) · 2.56 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
const errorIcons = document.getElementsByTagName('svg');
const submitButton = document.getElementById('submitButton');
const firstNameVal = document.querySelector('[data-namefield]')
const lastNameVal = document.querySelector('[data-lastNamefield]')
const emailVal = document.querySelector('[data-emailfield]')
const pwVal = document.querySelector('[data-pwfield]')
const successHandle = input => {
const errorIcon = input.parentElement.querySelector('svg');
const errorLabel = input.parentElement.querySelector('div');
input.classList.remove('error-border')
errorIcon.classList.remove('error')
errorLabel.classList.remove('error')
input.classList.add('success-border')
errorIcon.classList.add('success')
errorLabel.classList.add('success')
}
const errorHandle = (input, message) => {
const errorIcon = input.parentElement.querySelector('svg');
const errorLabel = input.parentElement.querySelector('div');
input.classList.remove('success-border')
errorIcon.classList.remove('success')
errorLabel.classList.remove('success')
input.classList.add('error-border')
errorIcon.classList.add('error')
errorLabel.classList.add('error')
errorLabel.innerText = message
}
const nameCheck = input => {
const nameRegex = /[0-9]/g;
if (input.value == '') {
errorHandle(input, 'Name cannot be empty!')
return;
}
if (input.value.search(nameRegex) === -1) {
successHandle(input)
} else {
errorHandle(input, 'Name cannot contain a number!')
}
}
const emailCheck = input => {
const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (input.value == '') {
errorHandle(input, 'Email cannot be empty!')
return;
}
if (emailRegex.test(String(input.value).toLowerCase())) {
successHandle(input)
} else {
errorHandle(input, 'Looks like this is not an email!')
input.value = 'email@example.com'
errorLabel.innerText = 'Looks like this is not an email!'
}
}
const pwCheck = input => {
if (input.value == '') {
errorHandle(input, 'Password cannot be empty!')
return;
}
if (input.value.length < 8) {
errorHandle(input, 'Password must be at least 8 characters!')
} else {
successHandle(input);
}
}
submitButton.addEventListener('click', (ev) => {
ev.preventDefault();
nameCheck(firstNameVal);
nameCheck(lastNameVal);
emailCheck(emailVal);
pwCheck(pwVal);
})