0% found this document useful (0 votes)
1 views3 pages

Form Validation Explanation

Form validation ensures that user-entered data meets required criteria before submission, enhancing data integrity and user experience. It can be categorized into client-side validation, which provides immediate feedback, and server-side validation, which is more secure. An example using JavaScript demonstrates how to validate name and email fields, preventing submission if they are empty or incorrectly formatted.

Uploaded by

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

Form Validation Explanation

Form validation ensures that user-entered data meets required criteria before submission, enhancing data integrity and user experience. It can be categorized into client-side validation, which provides immediate feedback, and server-side validation, which is more secure. An example using JavaScript demonstrates how to validate name and email fields, preventing submission if they are empty or incorrectly formatted.

Uploaded by

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

Form Validation - Explanation with Example

Form validation is the process of verifying that the data entered into a form by the user meets the

required criteria before it is submitted to the server. It ensures data integrity and enhances user

experience.

Types of Form Validation:

-------------------------

1. Client-Side Validation:

- Done using JavaScript or HTML5 attributes.

- Immediate feedback without a server round trip.

2. Server-Side Validation:

- Done on the server after the form submission.

- More secure as client-side validation can be bypassed.

Importance of Form Validation:

------------------------------

- Prevents incorrect data from being submitted.

- Improves data quality.

- Enhances user interaction.

- Reduces errors and server load.

Example: JavaScript Form Validation

-----------------------------------

<html>
<body>

<form id="form">

Name: <input type="text" id="name"><br>

Email: <input type="email" id="email"><br>

<input type="submit">

</form>

<script>

document.getElementById("form").addEventListener("submit", function(event) {

let name = document.getElementById("name").value;

let email = document.getElementById("email").value;

let error = "";

let sample = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;

if (name === "" || email === "") {

error += "Name and Email cannot be empty.\n";

} else if (!email.match(sample)) {

error += "Enter a valid email address.\n";

if (error !== "") {

event.preventDefault();

alert(error);

} else {

alert("Form submitted successfully!");

}
});

</script>

</body>

</html>

In this example, JavaScript is used to validate that:

- The name and email fields are not empty.

- The email follows a valid format.

If validation fails, an alert is shown and form submission is prevented.

You might also like