-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass49.html
More file actions
76 lines (70 loc) · 2.32 KB
/
class49.html
File metadata and controls
76 lines (70 loc) · 2.32 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Validation Example</title>
<style>
.error { color: red; font-size: 0.9em; }
input.error-border { border: 2px solid red; }
</style>
</head>
<body>
<form id="signupForm">
<label>
Name:
<input type="text" id="name" name="name" required>
<span class="error" id="nameError"></span>
</label>
<br><br>
<label>
Email:
<input type="email" id="email" name="email" required>
<span class="error" id="emailError"></span>
</label>
<br><br>
<label>
Password:
<input type="password" id="password" name="password" required minlength="6">
<span class="error" id="passwordError"></span>
</label>
<br><br>
<button type="submit">Sign Up</button>
</form>
<script>
document.getElementById("signupForm").addEventListener("submit", function (e) {
e.preventDefault(); // prevent form submission until validated
let isValid = true;
// Clear old errors
document.querySelectorAll(".error").forEach(el => el.textContent = "");
document.querySelectorAll("input").forEach(el => el.classList.remove("error-border"));
// Validate Name
const name = document.getElementById("name");
if (name.value.trim() === "") {
document.getElementById("nameError").textContent = "Name is required.";
name.classList.add("error-border");
isValid = false;
}
// Validate Email
const email = document.getElementById("email");
const emailPattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/i;
if (!emailPattern.test(email.value)) {
document.getElementById("emailError").textContent = "Enter a valid email.";
email.classList.add("error-border");
isValid = false;
}
// Validate Password
const password = document.getElementById("password");
if (password.value.length < 6) {
document.getElementById("passwordError").textContent = "Password must be at least 6 characters.";
password.classList.add("error-border");
isValid = false;
}
// Submit if all valid
if (isValid) {
alert("Form submitted successfully!");
this.submit();
}
});
</script>
</body>
</html>