Form Validation html elements
Form Validation html elements
Form validation is the process of checking the data entered by users in form
fields against specific criteria before submitting it to the server. This validation
process ensures that the input data is both complete and formatted correctly
according to predefined rules.
The primary goal of form validation is to ensure that invalid data does not get
processed, which can lead to erroneous operations, security risks, and a
degraded user experience.
Client-side form validation is used to ensure that the data entered by the user
is correct before submitting it to the server. This can be done using HTML
attributes and JavaScript for more complex validation logic.
Here’s an example that demonstrates the most common HTML form elements
along with client-side validation using HTML elements:
Example: Client-Side Form Validation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Client-Side Form Validation</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<h2>Registration Form</h2>
<form id="registrationForm" onsubmit="return
validateForm()">
<label for="username">Username (required, min 5
characters):</label><br>
<input type="text" id="username"
name="username" minlength="5" required>
<span id="usernameError"
class="error"></span><br><br>
<label for="confirmPassword">Confirm
Password:</label><br>
<input type="password" id="confirmPassword"
name="confirmPassword" required>
<span id="confirmPasswordError"
class="error"></span><br><br>
<label for="newsletter">Subscribe to
newsletter:</label><br>
<input type="checkbox" id="newsletter"
name="newsletter"><br><br>
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
let isValid = true;
// Validate username
const username =
document.getElementById("username").value;
if (username.length < 5) {
document.getElementById("usernameError").t
extContent = "Username must be at least 5 characters
long.";
isValid = false;
}
// Validate email
const email =
document.getElementById("email").value;
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-
Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!emailPattern.test(email)) {
document.getElementById("emailError").textC
ontent = "Please enter a valid email address.";
isValid = false;
}
// Validate password
const password =
document.getElementById("password").value;
if (password.length < 8) {
document.getElementById("passwordError").t
extContent = "Password must be at least 8 characters
long.";
isValid = false;
}
// Validate age
const age =
document.getElementById("age").value;
if (age < 18 || age > 100) {
document.getElementById("ageError").textCon
tent = "Age must be between 18 and 100.";
isValid = false;
}
return isValid;
}
</script>
</body>
</html>
1. HTML Form Elements:
o <input> elements: These are used for gathering user input like
text, email, password, etc.
o required: This attribute makes the field mandatory.
o minlength and maxlength: These attributes specify the minimum
and maximum length for input fields.
o type="email": Ensures that the input is a valid email format.
o type="number": Ensures that the input is numeric and can be
constrained with min and max.
2. JavaScript Validation:
o The validateForm() function is called when the form is submitted
(onsubmit="return validateForm()").
o The function checks each field for validity and displays error
messages if the validation fails.
o If any validation fails, the isValid flag is set to false, and the form
submission is prevented by returning false.
3. Error Display:
o Error messages are displayed in <span> elements with a class of
"error", which are styled in red.
o These error messages are dynamically set by JavaScript based on
the validation outcome.