0% found this document useful (0 votes)
71 views8 pages

Program For Form Validation

This PHP code validates form input data by checking for empty fields and validating formats. It uses regular expressions to check the name, email, and website fields and outputs any errors. On form submission, the input is tested and sanitized then output below the form along with any caught errors.

Uploaded by

Selva Kumar
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)
71 views8 pages

Program For Form Validation

This PHP code validates form input data by checking for empty fields and validating formats. It uses regular expressions to check the name, email, and website fields and outputs any errors. On form submission, the input is tested and sanitized then output below the form along with any caught errors.

Uploaded by

Selva Kumar
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/ 8

/*PROGRAM FOR FORM VALIDATION*/

<html>
<head>
<style>
.error{code:#ff000;}
</style>
</head>
<body>
<?php
$nameErr=$emailErr=$genderErr=$websiteErr="";
$name=$email=$gender=$comment=$website="";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(empty($_POST["name"]))
{
$nameErr=" ";
}
else
{
$name=test_input($_POST["name"]);
if(!preg_match("/^[a-zA-Z]*$/",$name))
{
$nameErr="Only letters and white space allowed";
}
}
}
if(empty($_POST["email"]))
{
$emailErr=" ";
}
else
{
$email=test_input($_POST["email"]);
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$emailErr="Invalid email format";
}
}
if(empty($_POST["website"]))
{
$website="";
}
else
{
$website=test_input($_POST["website"]);
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=`~_|!:,.;]*[-a-z0-
9+&@#\/%=~_\]/i",$website))
{
$websiteErr="invalid URL";

}
}
if(empty($_POST["comment"]))
{
$comment="";
}
else
{
$comment=test_input($_POST["comment"]);
}
if(empty($_POST["gender"]))
{
$genderErr=" ";
}
else
{
$gender=test_input($_POST["gender"]);
}
function test_input($data)
{
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
?>

<h2> Form Validation </h2>


<p>
<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>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">*<?php echo $emailErr;?> </span>
<br> <br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?> </span>
<br> <br>
Comment: <textarea nzme="comment" rows="5" cols="40"><?php echo $comment;?>
</textarea>
<br> <br>
Gender: <input type="radio" name="gender"<?php if(isset($gender)&&
$gender=="female") echo "checked";?> value="female">female
<input type="radio" name="gender" <?php if(isset($gender)&& $gender=="male") echo
"checked";?> value="male">male
<span class="error">*<?php echo $genderErr;?></span>
<br> <br>
<input type="submit" name="submit" value="submit">
</form>

<?php
echo "<h2> Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
OUTPUT:
RUN1:
RUN2:

You might also like