-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14-Forms-Handling-Validation.html
More file actions
127 lines (105 loc) · 3.17 KB
/
14-Forms-Handling-Validation.html
File metadata and controls
127 lines (105 loc) · 3.17 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>14-Forms-Handling-Validation</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
form {
width: 50%;
padding: 35px 0;
border: 1px solid #000;
border-radius: 5px;
}
input {
display: block;
width: 70%;
margin: auto;
padding: 5px 10px;
font-size: 18px;
border: 1px solid #000;
border-radius: 5px;
}
button {
display: block;
width: 50%;
margin: 0 auto;
padding: 10px;
background-color: #000;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
#error {
width: fit-content;
margin: 10px auto;
padding: 5px 20px;
background-color: rgb(209, 64, 64);
color: #fff;
font-size: 20px;
text-align: center;
}
</style>
</head>
<body>
<form id="signupForm">
<input type="text" id="username" placeholder="Enter username" />
<br /><br />
<input type="email" id="email" placeholder="Enter email" />
<br /><br />
<input type="password" id="password" placeholder="Enter password" />
<br /><br />
<button type="submit">Sign Up</button>
<p id="error"></p>
</form>
<script>
// Selecting the form element using its ID
const form = document.getElementById("signupForm");
// Selecting the paragraph element where error messages will be shown
const error = document.getElementById("error");
// Adding a submit event listener to the form
form.addEventListener("submit", function (event) {
// Prevents the default form behavior (page reload)
event.preventDefault();
// Getting values entered by the user
const username = document.getElementById("username").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
// Printing input values in console (for debugging / learning)
console.log(username, email, password);
// Clearing any previous error message
error.textContent = "";
// Validation: Check if username is empty
if (username === "") {
error.textContent = "Username is required";
return; // Stop further execution
}
// Validation: Check if email is empty
if (email === "") {
error.textContent = "Email is required";
return;
}
// Validation: Check password length
if (password.length < 6) {
error.textContent = "Password must be at least 6 characters";
return;
}
// If all validations pass
alert("Form Submitted Successfully");
// Logging success message in console
console.log("Form Submitted");
});
</script>
</body>
</html>