forked from Manav39/INH-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
99 lines (82 loc) · 3.24 KB
/
update.js
File metadata and controls
99 lines (82 loc) · 3.24 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
89
90
91
92
93
94
95
96
97
98
99
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('registrationForm');
form.addEventListener('submit', (event) => {
event.preventDefault();
clearErrors();
const validationResult = validateForm();
if (validationResult.isValid) {
handleFormSubmission(validationResult.formData);
} else {
displayErrors(validationResult.errors);
}
});
function validateForm() {
const username = document.getElementById('username').value.trim();
const fullName = document.getElementById('fullName').value.trim();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirmPassword').value;
const errors = [];
if (username.length < 3) {
errors.push({
field: 'username',
message: 'Username must be at least 3 characters long'
});
}
if (fullName.length < 2) {
errors.push({
field: 'fullName',
message: 'Please enter your name'
});
}
if (!validateEmail(email)) {
errors.push({
field: 'email',
message: 'Please enter a valid email address'
});
}
if (password.length < 8) {
errors.push({
field: 'password',
message: 'Password must be at least 8 characters long'
});
}
if (password !== confirmPassword) {
errors.push({
field: 'confirmPassword',
message: 'Passwords do not match'
});
}
return {
isValid: errors.length === 0,
errors: errors,
formData: errors.length === 0 ? { username, fullName, email, password } : null
};
}
function validateEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
function displayErrors(errors) {
errors.forEach(error => {
const field = document.getElementById(error.field);
const formGroup = field.closest('.form-group');
const errorDiv = document.createElement('div');
errorDiv.className = 'error-message';
errorDiv.textContent = error.message;
field.classList.add('input-error');
formGroup.appendChild(errorDiv);
});
}
function clearErrors() {
const errorMessages = document.querySelectorAll('.error-message');
errorMessages.forEach(msg => msg.remove());
const errorInputs = document.querySelectorAll('.input-error');
errorInputs.forEach(input => input.classList.remove('input-error'));
}
function handleFormSubmission(formData) {
console.log('Form submitted:', formData);
form.reset();
alert('Account created successfully!');
}
});