-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformValidation.html
96 lines (78 loc) · 2.98 KB
/
formValidation.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Form Validation</title>
<style>
form {
width: 300px;
margin: auto;
}
input {
width: 100%;
margin: 10px 0;
padding: 8px;
}
.error {
color: red;
font-size: 12px;
}
</style>
</head>
<body>
<h2 style="text-align: center;">Form Validation</h2>
<form id="myForm" onsubmit="return validateForm()">
<input type="text" id="name" placeholder="Name (A-Z)">
<span class="error" id="nameError"></span>
<input type="number" id="age" placeholder="Age (0-100)">
<span class="error" id="ageError"></span>
<input type="text" id="email" placeholder="Email (must contain @)">
<span class="error" id="emailError"></span>
<input type="password" id="password" placeholder="Password">
<span class="error" id="passwordError"></span>
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
const name = document.getElementById("name").value.trim();
const age = document.getElementById("age").value.trim();
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
const nameError = document.getElementById("nameError");
const ageError = document.getElementById("ageError");
const emailError = document.getElementById("emailError");
const passwordError = document.getElementById("passwordError");
// Clear previous error messages
nameError.textContent = "";
ageError.textContent = "";
emailError.textContent = "";
passwordError.textContent = "";
let isValid = true;
// Validate Name
if (!/^[A-Za-z]+$/.test(name)) {
nameError.textContent = "Name must only contain letters (A-Z).";
isValid = false;
}
// Validate Age
if (age === "" || age < 0 || age > 100) {
ageError.textContent = "Age must be between 0 and 100.";
isValid = false;
}
// Validate Email
if (!email.includes("@")) {
emailError.textContent = "Email must contain '@'.";
isValid = false;
}
// Validate Password
const passwordRegex = /^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{6,}$/;
if (!passwordRegex.test(password)) {
passwordError.textContent =
"Password must include 1 uppercase letter, 1 number, 1 special character, and be at least 6 characters.";
isValid = false;
}
return isValid;
}
</script>
</body>
</html>