Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (75 loc) · 2.38 KB
/
Copy pathscript.js
File metadata and controls
88 lines (75 loc) · 2.38 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
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
// Function to Check Error
function showError(input, message) {
const subForm = input.parentElement;
subForm.className = 'sub-Form error';
const small = subForm.querySelector('small');
small.innerText = message;
}
// Function to Show Success
function showSuccess(input) {
const subForm = input.parentElement;
subForm.className = 'sub-Form success';
}
// Check if Email is Valid (regex)
function isEmail(email) {
const valid = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
return valid.test(email);
}
// Check Password Strength (regex)
function passwordStrength(password) {
const strengthPass = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
return strengthPass.test(password);
}
// Main function
form.addEventListener('submit', (event) => {
event.preventDefault();
checkingForm();
});
//Function to check all of input
function checkingForm() {
// Username
if (username.value === '') {
showError(username, 'Username cannot be empty');
} else {
showSuccess(username);
}
// Email
if (email.value === '') {
showError(email, 'Email cannot be empty');
} else if (!isEmail(email.value)) {
showError(email, 'Email is not valid');
} else {
showSuccess(email);
}
// Password
if (password.value === '') {
showError(password, 'Password cannot be empty');
} else if (password.value.length < 8) {
showError(password, 'Password must be at least 8 characters');
} else if (!passwordStrength(password.value)) {
showError(
password,
'Password must contain at least one uppercase letter, one lowercase letter, and one digit'
);
} else {
showSuccess(password);
displayAlert();
}
}
// Display an alert to show 'Sign up Successful'
function displayAlert(){
const name = username.value
if(username.value !== '' & email.value !== ''){
Swal.fire({
title: `Hello, ${name}`,
text: 'Welcome! We\'re excited to have you join us',
icon: 'success',
timer: 3000, // automatically closed after 3s (time in milliseconds)
showConfirmButton: false // don't show confirm button
});
}
}