-
Notifications
You must be signed in to change notification settings - Fork 0
/
9-DOMForm.html
104 lines (92 loc) · 3.57 KB
/
9-DOMForm.html
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
<!DOCTYPE html>
<html>
<head>
<title>Form Validation Example</title>
<style>
.error {
color: red;
margin-top: 5px;
}
</style>
</head>
<body>
<h1>Form Validation Example</h1>
<form id="myForm">
<div>
<label for="name">Name:</label>
<input type="text" id="name" required />
<div class="error"></div>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" required />
<div class="error"></div>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" required />
<div class="error"></div>
</div>
<div>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" required />
<div class="error"></div>
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>
<script>
window.onload = function () {
const form = document.getElementById('myForm');
const nameInput = document.getElementById('name');
const emailInput = document.getElementById('email');
const passwordInput = document.getElementById('password');
const confirmPasswordInput = document.getElementById('confirmPassword');
const errorMessages = document.getElementsByClassName('error');
form.addEventListener('submit', function (event) {
event.preventDefault();
resetErrorMessages();
if (!validateName(nameInput.value)) {
showError(nameInput, 'Name should not be empty and should contain only letters.');
}
if (!validateEmail(emailInput.value)) {
showError(emailInput, 'Invalid email address.');
}
if (!validatePassword(passwordInput.value)) {
showError(
passwordInput,
'Password should have a minimum length of 8 characters and contain at least one uppercase letter, one lowercase letter, and one digit.'
);
}
if (!validateConfirmPassword(passwordInput.value, confirmPasswordInput.value)) {
showError(confirmPasswordInput, 'Confirm password does not match the password.');
}
});
function validateName(name) {
return /^[A-Za-z]+$/.test(name);
}
function validateEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function validatePassword(password) {
return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/.test(password);
}
function validateConfirmPassword(password, confirmPassword) {
return password === confirmPassword;
}
function showError(inputElement, errorMessage) {
inputElement.classList.add('error');
const errorElement = inputElement.nextElementSibling;
errorElement.textContent = errorMessage;
}
function resetErrorMessages() {
for (let i = 0; i < errorMessages.length; i++) {
const errorElement = errorMessages[i];
errorElement.textContent = '';
const inputElement = errorElement.previousElementSibling;
inputElement.classList.remove('error');
}
}
};
</script>