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

Untitled Document

Nil

Uploaded by

Nivedha S
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)
10 views3 pages

Untitled Document

Nil

Uploaded by

Nivedha S
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

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation Example</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<?php
// Initialize variables
$name = $email = $age = $phone = "";
$nameErr = $emailErr = $ageErr = $phoneErr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate Name
if (empty($_POST["name"])) {
$nameErr = "*Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = "Only letters and whitespaces allowed";
}
}

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

// Validate Age
if (empty($_POST["age"])) {
$ageErr = "*Age is required";
} else {
$age = test_input($_POST["age"]);
if (!is_numeric($age)) {
$ageErr = "Age should be a number";
}
}

// Validate Phone Number


if (empty($_POST["phone"])) {
$phoneErr = "*Phone number is required";
} else {
$phone = test_input($_POST["phone"]);
if (!preg_match("/^[0-9]{10}$/", $phone)) {
$phoneErr = "Phone number must be a 10-digit number";
}
}

// If no errors, display success message


if (empty($nameErr) && empty($emailErr) && empty($ageErr) && empty($phoneErr)) {
echo "<h3>Form submitted successfully!</h3>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>Age: $age</p>";
echo "<p>Phone: $phone</p>";
}
}

// Function to sanitize input


function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>">
<span class="error"><?php echo $nameErr; ?></span><br><br>

<label for="email">E-mail:</label>
<input type="email" id="email" name="email" value="<?php echo $email; ?>">
<span class="error"><?php echo $emailErr; ?></span><br><br>

<label for="age">Age:</label>
<input type="text" id="age" name="age" value="<?php echo $age; ?>">
<span class="error"><?php echo $ageErr; ?></span><br><br>

<label for="phone">Phone Number:</label>


<input type="text" id="phone" name="phone" value="<?php echo $phone; ?>">
<span class="error"><?php echo $phoneErr; ?></span><br><br>

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


</form>
</body>
</html>

You might also like