-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (68 loc) · 2.84 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
function handleSignupFormSubmit(e) {
e.preventDefault();
}
const signupForm = document.getElementById('signupForm');
const submitButton = document.getElementById('submit');
submitButton.addEventListener('click', function(e) {
e.preventDefault();
console.log('Form submission event listener triggered!');
const formData = new FormData(signupForm);
const firstName = formData.get('firstName');
const lastName = formData.get('lastName');
const email = formData.get('email');
const password = formData.get('password');
console.log('firstName', firstName);
console.log('lastName', lastName);
console.log('password', password);
console.log('email', email);
const firstNameErrorMessage = validateFirstName(firstName);
const lastNameErrorMessage = validateLastName(lastName);
const emailErrorMessage = validateEmail(email);
const passwordErrorMessage = validatePassword(password);
const firstNameErrorMessageElement = document.getElementById('first-name-message');
firstNameErrorMessageElement.innerText = firstNameErrorMessage;
const lastNameErrorMessageElement = document.getElementById('last-name-message');
lastNameErrorMessageElement.innerText = lastNameErrorMessage;
const emailErrorMessageElement = document.getElementById('email-message');
emailErrorMessageElement.innerText = emailErrorMessage;
const passwordErrorMessageElement = document.getElementById('password-message');
passwordErrorMessageElement.innerText = passwordErrorMessage;
});
function validateFirstName(firstName) {
if (!firstName) {
return 'First Name is required';
}
return '';
}
function validateLastName(lastName) {
if(!lastName) {
return 'Last Name is required';
}
return '';
}
function validateEmail(email) {
if (!email) return 'Email is required';
const isValidEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!isValidEmail.test(email)) {
return 'Please enter a valid email';
}
return '';
}
function validatePassword(password) {
const minLength = 10;
if (!password) {
return 'Password is required';
}
if (password.length < minLength) {
return `Please enter a password that is at least ${minLength} characters long`;
}
const hasCapitalLetter = /[A-Z]/;
if (!hasCapitalLetter.test(password)) {
return 'Please use at least one capital letter';
}
const hasNumber = /\d/;
if (!hasNumber.test(password)) {
return 'Please use at least one number';
}
return '';
}