-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
82 lines (68 loc) · 2.11 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
const btn = document.getElementById('btn');
const username = document.getElementById('username');
const email = document.getElementById('email');
const pass = document.getElementById('pass');
const vpass = document.getElementById('vpass');
let user = undefined;
let em = undefined;
let pas = undefined;
let vpas = undefined;
btn.addEventListener('click', e => {
checkData();
if (user !== true || em !== true || pas !== true || vpass !== true) {
e.preventDefault();
}
});
function checkData() {
const usernameValue = username.value.trim();
const emailValue = email.value.trim();
const passwordValue = pass.value.trim();
const vpasswordValue = vpass.value.trim();
if (usernameValue === '') {
setError(username, 'Username cannot be blank');
user = false;
}else{
setNoError(username);
user = true;
}
if (emailValue === '') {
setError(email, 'Email cannot be blank');
em = false;
}else if (!rEmail(emailValue)) {
setError(email, 'Email is not a valid');
em = false;
}else{
setNoError(email);
em = true;
}
if (passwordValue === '') {
setError(pass, 'Password cannot be blank');
pas = false;
}else{
setNoError(pass);
pas = true;
}
if (vpasswordValue === '') {
setError(vpass,'Password cannot be blank');
vpas = false;
} else if (passwordValue !== vpasswordValue) {
setError(vpass, 'Password does not match');
vpas = false;
}else{
setNoError(vpass);
vpas = true;
}
}
function setError(input, message) {
const formLabel = input.parentElement;
const small = formLabel.querySelector('small');
formLabel.className = 'form-label error';
small.innerText = message;
}
function setNoError(input) {
const formLabel = input.parentElement;
formLabel.className = 'form-label noerror';
}
function rEmail(email) {
return /^(([^<>()\[\]\\.,;:\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,}))$/.test(email);
}