0% found this document useful (0 votes)
2 views

Form Validation html elements

Form validation is the process of checking user input in forms against specific criteria to ensure data integrity and security. Client-side validation is implemented using HTML attributes and JavaScript to provide immediate feedback on input errors before submission. The document includes an example of a registration form with various input types and validation rules, along with JavaScript functions to validate the data and display error messages.

Uploaded by

ranjanarao25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Form Validation html elements

Form validation is the process of checking user input in forms against specific criteria to ensure data integrity and security. Client-side validation is implemented using HTML attributes and JavaScript to provide immediate feedback on input errors before submission. The document includes an example of a registration form with various input types and validation rules, along with JavaScript functions to validate the data and display error messages.

Uploaded by

ranjanarao25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1 WHAT IS FORM VALIDATION?

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="email">Email (required, valid


format):</label><br>
<input type="email" id="email" name="email"
required>
<span id="emailError"
class="error"></span><br><br>

<label for="password">Password (required, min 8


characters):</label><br>
<input type="password" id="password"
name="password" minlength="8" required>
<span id="passwordError"
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="age">Age (required, number between


18 and 100):</label><br>
<input type="number" id="age" name="age"
min="18" max="100" required>
<span id="ageError"
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;

// Clear previous errors


document.getElementById("usernameError").text
Content = "";
document.getElementById("emailError").textCon
tent = "";
document.getElementById("passwordError").text
Content = "";
document.getElementById("confirmPasswordErro
r").textContent = "";
document.getElementById("ageError").textConte
nt = "";

// 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 confirm password


const confirmPassword =
document.getElementById("confirmPassword").value;
if (password !== confirmPassword) {
document.getElementById("confirmPasswordE
rror").textContent = "Passwords do not match.";
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.

Common HTML Form Elements with Validation:


 Text inputs: <input type="text">
 Email inputs: <input type="email"> (requires a valid email format)
 Password inputs: <input type="password"> (hidden text for password
fields)
 Checkboxes: <input type="checkbox">
 Radio buttons: <input type="radio">
 Number inputs: <input type="number"> (validates number within a
specified range)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy