-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (85 loc) · 2.74 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
document.getElementById('signupForm').addEventListener('submit',(e)=>{
if(!validateForm()){
e.preventDefault();
}
});
function validateForm(){
const validations = [
validateUsername,
validateEmail,
validatePassword,
validateConfirmPassword
];
let valid = true;
for (const validate of validations) {
valid = validate() && valid;
}
return valid;
}
function validateUsername() {
const username = document.getElementById('username').value;
const usernameError = document.getElementById('username-error');
usernameError.textContent = "";
if (username === "") {
usernameError.textContent = "Username must be fill out";
return false;
} else {
return true;
}
}
function validateEmail(){
const email = document.getElementById('email').value;
const emailError = document.getElementById('email-error');
const emailPattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;
emailError.textContent = "";
if(email === ""){
emailError.textContent="Email must be fill out";
return false;
}
else if(!email.match(emailPattern)){
emailError.textContent = "Please enter a valid email address";
}
else{
return true;
}
}
function validatePassword(){
const password = document.getElementById('password').value;
const passwordError = document.getElementById('password-error');
passwordError.textContent = "";
if(password === ""){
passwordError.textContent = "Password must be fill out";
return false;
}
else if(password.length < 8){
passwordError.textContent = "Password must be at least 8 characters long";
}
else if(!/[A-Z]/.test(password)){
passwordError.textContent = "Password must contain at least one uppercase letter";
}
else if(!/\d/.test(password)){
passwordError.textContent = "Password must contain at least one number";
}
else if(!/[!@#$%^&*]/.test(password)){
passwordError.textContent = "Password must contain at least one symbol";
}
else{
return true;
}
}
function validateConfirmPassword(){
const confirmPassword = document.getElementById('confirm-password').value;
const password = document.getElementById('password').value;
const confirmPasswordError = document.getElementById('confirm-password-error');
confirmPasswordError.textContent = "";
if(confirmPassword === ""){
confirmPasswordError.textContent = "Confirm password must be fill out";
return false;
}
else if(password !== confirmPassword){
confirmPasswordError.textContent = "Confirm password do not match";
}
else{
return true;
}
}