0% found this document useful (0 votes)
6 views4 pages

INT 220 - 7 - Badal

This document contains a PHP script for a form validation that checks user input for name, email, and password fields. It includes error handling for empty fields and validates the format of the email and the length of the password. Upon successful submission, it displays a confirmation message along with the entered name and email.

Uploaded by

skilsoftwares
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)
6 views4 pages

INT 220 - 7 - Badal

This document contains a PHP script for a form validation that checks user input for name, email, and password fields. It includes error handling for empty fields and validates the format of the email and the length of the password. Upon successful submission, it displays a confirmation message along with the entered name and email.

Uploaded by

skilsoftwares
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/ 4

INT 220- WEEK 7

NAME: badal
Roll no:42
Reg no:12303349
SECTION: K23DJ
Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Form Validation</title>
<style>
.error { color: red; }
</style>
</head>
<body>

<?php
$name = $email = $password = "";
$nameErr = $emailErr = $passwordErr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = sanitize_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z-' ]*$/", $name)) {
$nameErr = "Only letters and spaces allowed";
}
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = sanitize_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = sanitize_input($_POST["password"]);
if (strlen($password) < 6) {
$passwordErr = "Password must be at least 6 characters long";
}
}
}

function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Name: <input type="text" name="name" value="<?php echo $name; ?>">
<span class="error">* <?php echo $nameErr; ?></span>
<br><br>

Email: <input type="text" name="email" value="<?php echo $email; ?>">


<span class="error">* <?php echo $emailErr; ?></span>
<br><br>

Password: <input type="password" name="password">


<span class="error">* <?php echo $passwordErr; ?></span>
<br><br>

<input type="submit" value="Submit">


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && empty($nameErr) &&
empty($emailErr) && empty($passwordErr)) {
echo "<h3>Form Submitted Successfully</h3>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
}
?>

</body>
</html

Output:

You might also like