-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignUpValidations.js
97 lines (83 loc) · 3.47 KB
/
SignUpValidations.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
document.addEventListener('DOMContentLoaded', function () {
const form = document.getElementById('signup_form');
const fullname = document.getElementById('fullname');
const email = document.getElementById('email');
const confirmEmail = document.getElementById('confirm_email');
const password = document.getElementById('password');
const confirmPassword = document.getElementById('confirm_password');
form.addEventListener('submit', function (event) {
event.preventDefault(); // Prevents form submission
// Clear all previous error messages
clearErrorMessages();
// Validate the form
if (validateForm()) {
// Redirect to main page here
window.location.href = 'Blog.html';
} else {
// Display general error message
showError('Form validation failed');
}
});
function validateForm() {
let isValid = true;
// Validate full name
if (fullname.value.trim() === '') {
showError('Please enter your full name', 'fullname-error');
isValid = false;
} else if (!isValidFullName(fullname.value)) {
showError('Full name should only contain letters, spaces, and periods', 'fullname-error');
isValid = false;
}
// Validate email
if (email.value.trim() === '') {
showError('Please enter your email address', 'email-error');
isValid = false;
} else if (!isValidEmail(email.value)) {
showError('Please enter a valid email address', 'email-error');
isValid = false;
}
// Validate confirm email
if (confirmEmail.value === '') {
showError('Please confirm your email address', 'confirm-email-error');
isValid = false;
} else if (email.value !== confirmEmail.value) {
showError('Email confirmation does not match', 'confirm-email-error');
isValid = false;
}
// Validate password
if (password.value === '') {
showError('Please enter your password', 'password-error');
isValid = false;
} else if (password.value.length < 8) {
showError('Password should be at least 8 characters long', 'password-error');
isValid = false;
}
// Validate confirm password
if (confirmPassword.value === '') {
showError('Please confirm your password', 'confirm-password-error');
isValid = false;
} else if (password.value !== confirmPassword.value) {
showError('Password confirmation does not match', 'confirm-password-error');
isValid = false;
}
return isValid;
}
function isValidFullName(value) {
// Check if the value contains only letters, spaces, and periods
return /^[a-zA-Z\s.]+$/.test(value);
}
function isValidEmail(value) {
// Simple email validation (checking for @ symbol)
return value.includes('@');
}
function showError(message, errorId) {
const errorElement = document.getElementById(errorId);
errorElement.textContent = message;
}
function clearErrorMessages() {
const errorMessages = document.querySelectorAll('.error-message');
errorMessages.forEach(message => {
message.textContent = '';
});
}
});