-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpraroz_signup.js
More file actions
86 lines (67 loc) · 2.4 KB
/
Copy pathpraroz_signup.js
File metadata and controls
86 lines (67 loc) · 2.4 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
const form = document.getElementById('signForm');
const email = document.getElementById('email');
const uname = document.getElementById('username');
const password = document.getElementById('password');
const cpassword = document.getElementById('confirmpassword');
// Password and email criteria/standard
function isEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function isValidPassword(password) {
return /[A-Za-z\d@$!%?_&]{8,}$/.test(password);
}
form.addEventListener("submit", function (e) {
e.preventDefault();
checkInput();
});
// Input value validation
function checkInput() {
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
const cpasswordValue = cpassword.value.trim();
const unameValue = uname.value.trim();
// email address validation
if (emailValue === '') {
setErrorfor(email, 'Email cannot be blank');
} else if (!isEmail(emailValue)) {
setErrorfor(email, 'Invalid email address');
} else {
setSuccessfor(email);
}
// Username Validation
if (unameValue === '') {
setErrorfor(uname, 'Username cannot be blank');
} else if (unameValue.length < 4) {
setErrorfor(uname, 'Username cannot be less than 4 characters');
} else {
setSuccessfor(uname);
}
// password validation
if (passwordValue === '') {
setErrorfor(password, 'Password cannot be blank');
} else if (!isValidPassword(passwordValue)) {
setErrorfor(password, 'Password must contain an uppercase letter, a lowercase letter, a special character, and a number');
} else if (passwordValue.length < 8) {
setErrorfor(password, 'Password cannot be less than eight characters');
} else {
setSuccessfor(password);
}
// Password check
if (cpasswordValue === '') {
setErrorfor(cpassword, 'Password cannot be blank');
} else if (cpasswordValue != passwordValue) {
setErrorfor(cpassword, 'Passwords do not match');
} else {
setSuccessfor(cpassword);
}
}
function setErrorfor(input, message) {
const formcontrol = input.parentElement;
const small = formcontrol.querySelector('small');
small.innerText = message;
formcontrol.className = 'form-control error';
}
function setSuccessfor(input) {
const formcontrol = input.parentElement;
formcontrol.className = 'form-control success';
}