Certainly! Form validation is a crucial aspect of web development to ensure data integrity and security. PHP is often used to validate data on the server side after it's been submitted through a form.
<form action="validate.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Submit">
<?php
$errors = [];
// Check if name is set and not empty
if (isset($_POST['name']) && !empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$errors[] = "Name is required!";
}
// Check if email is set, not empty and is valid
if (isset($_POST['email']) && !empty($_POST['email'])) {
$email = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Email format is invalid!";
}
} else {
$errors[] = "Email is required!";
}
// Check if password is set, not empty and at least 8 characters
if (isset($_POST['password']) && !empty($_POST['password'])) {
$password = $_POST['password'];
if (strlen($password) < 8) {
$errors[] = "Password should be at least 8 characters!";
}
} else {
$errors[] = "Password is required!";
}
// If there are errors, display them
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error . "<br>";
}
} else {
echo "Form is valid!";
// You can now process the form data.
}
?>
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here