जब HTML Form को create किया जाता है तब उसे validate भी किया जाता है | Validate करने का मतलब यही है कि जो भी User इस form को fill कर रहा है वो सही information डाले |
अगर User से valid information send करनी हो तो कुछ चीजों पर ध्यान रखना पड़ता है |
- Name : Name के सिर्फ letters और whitespace valid होंगे |
- UserName : UserName में सिर्फ letters और numbers ही valid होंगे |
- Email : Email में letters या nummbers उसके बाद @ symbol उसके बाद letters उसके बाद . (dot) और आखिरी में letters
- URL : URL valid होगा |
- Mobile Number : सिर्फ Number valid और length 10 digit तक होगी |
- Gender : Radio तो एक select होगा |
- Comment : Comment required नहीं है |
Name Validation
Source Code :Output :1234567891011121314151617181920<?php
$nameerror ="";
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
$nameerror = "Name is required";
}else{
$name = $_POST["name"];
if(!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameerror = "Only Letters and White Space allowed";
}}}
?>
<form action="" method="post">
Name : <input type="text" name="name" ><?php echo $nameerror; ?>
<input type="submit" name="submit" />
<?php
if(isset($_POST["submit"])){
echo "Name : ".$_POST["name"];
}
?>
Click to Name : Form Validation
UserName Validation
Source Code :Output :1234567891011121314151617181920<?php
$usernameerror ="";
if(isset($_POST['submit'])){
if (empty($_POST["username"])) {
$usernameerror = "UserName is required";
}else{
$username = $_POST["username"];
if(!preg_match("/^[a-zA-Z0-9]+$/", $username)) {
$usernameerror = "Only Letters and Numbers allowed";
}}}
?>
<form action="" method="post">
UserName : <input type="text" name="username" ><?php echo $usernameerror; ?><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
echo "UserName : ".$_POST["username"];
}
?>
Click to UserName : Form Validation
Email Validation
Source Code :Output :1234567891011121314151617181920<?php
$emailerror ="";
if(isset($_POST['submit'])){
if (empty($_POST["email"])) {
$emailerror = "Email is required";
}else{
$email = $_POST["email"];
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
$emailerror = "Invalid Email Format";
}}}
?>
<form action="" method="post">
Email : <input type="text" name="email" ><?php echo $emailerror; ?><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
echo "Email : ".$_POST["email"];
}
?>
Click to Email : Form Validation
URL Validation
Source Code :Output :1234567891011121314151617181920<?php
$urlerror ="";
if(isset($_POST['submit'])){
if (empty($_POST["URL"])) {
$urlerror = "URL is required";
}else{
$url = $_POST["URL"];
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
$urlerror = "Invalid URL Format";
}}}
?>
<form action="" method="post">
URL : <input type="text" name="URL" ><?php echo $urlerror; ?><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
echo "URL : ".$_POST["URL"];
}
?>
Click to URL : Form Validation
Mobile Number Validation
Source Code :Output :1234567891011121314151617181920<?php
$mobnoerror ="";
if(isset($_POST['submit'])){
if (empty($_POST["mobno"])) {
$mobnoerror = "Mobile Number is required";
}else{
$mobno = $_POST["mobno"];
if(!preg_match("/^\d{10}$/", $mobno)) {
$mobnoerror = "Only Numbers with 10 Digits required";
}}}
?>
<form action="" method="post">
Mobile No. : <input type="text" name="mobno" ><?php echo $mobnoerror; ?><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
echo "Mobile No. : ".$_POST["mobno"];
}
?>
Click to Mobile No. : Form Validation
Gender Required
Source Code :Output :12345678910111213141516<?php
$gendererror ="";
if(isset($_POST['submit'])){
if(empty($_POST["gender"])) {
$gendererror = " Gender is required";
}}
?>
<form action="" method="post">
<input class="radio" name="gender" type="radio" value="female">Female
<input class="radio" name="gender" type="radio" value="male">Male <?php echo $gendererror; ?><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["gender"]))
echo $_POST["gender"];
?>
Click to Gender : Form Validation
Comment No Validation
Source Code :Output :1234567891011121314151617<?php
$comment ="";
if (isset($_POST["submit"])){
if (empty($_POST["comment"])) {
$comment = "";
}}
?>
<form action="" method="post">
Comment :
<textarea cols="50" name="comment" rows="5">
</textarea><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["comment"]))
echo $_POST["comment"];
?>
Click to Comment : No Form Validation
Full Example For Form Validation
Source Code :Output :123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109<?php
$nameerror ="";
$usernameerror="";
$emailerror ="";
$mobnoerror = "";
$gendererror ="";
$urlerror ="";
$comment="";
if(isset($_POST['submit'])){
if(empty($_POST["name"])){
$nameerror = "Name is required";
}else{
$name = $_POST["name"];
if(!preg_match("/^[a-zA-Z ]*$/",$name)){
$nameerror = "Only Letters and White Space allowed";
}}
if (empty($_POST["username"])){
$usernameerror = "UserName is required";
}else{
$username = $_POST["username"];
if(!preg_match("/^[a-zA-Z0-9]+$/", $username)){
$usernameerror = "Only Letters and Numbers allowed";
}}
if (empty($_POST["email"])){
$emailerror = "Email is required";
}else{
$email = $_POST["email"];
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){
$emailerror = "Invalid Email Format";
}}
if (empty($_POST["URL"])){
$urlerror = "URL is required";
}else{
$url = $_POST["URL"];
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)){
$urlerror = "Invalid URL Format";
}}
if (empty($_POST["mobno"])){
$mobnoerror = "Mobile Number is required";
}else{
$mobno = $_POST["mobno"];
if(!preg_match("/^\d{10}$/", $mobno)){
$mobnoerror = "Only Numbers with 10 Digits required";
}}
if(empty($_POST["gender"])){
$gendererror = " Gender is required";
}
if (empty($_POST["comment"])){
$comment = "";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<style type="text/css">
.error{
color:#F00;
}
</style>
</head>
<body>
<h2>Form Validation</h2>
<form action="" method="post">
<span class="error">* required field.</span><br />
Name:
<input class="input" name="name" type="text" value="">
<span class="error">* <?php echo $nameerror;?></span><br />
UserName :
<input class="input" name="username" type="text" value="">
<span class="error">* <?php echo $usernameerror;?></span><br />
E-mail:
<input class="input" name="email" type="text" value="">
<span class="error">* <?php echo $emailerror;?></span><br />
URL :
<input class="input" name="URL" type="text" value="">
<span class="error">* <?php echo $urlerror;?></span><br />
Mobile Number :
<input class="input" name="mobno" type="text" value="">
<span class="error">* <?php echo $mobnoerror;?></span><br />
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">*<?php echo $gendererror; ?></span><br />
Comment:
<textarea cols="40" name="comment" rows="5">
</textarea><br />
<input class="submit" name="submit" type="submit" value="Submit">
</form>
<br />
<h2>Information</h2>
<?php
if(isset($_POST["submit"])){
echo "Name : ".$_POST["name"]."<br />";
echo "UserName : ".$_POST["username"]."<br />";
echo "Email : ".$_POST["email"]."<br />";
echo "URL : ".$_POST["URL"]."<br />";
echo "Mobile No : ".$_POST["mobno"]."<br />";
echo "Gender : ".$_POST["gender"]."<br />";
echo "Comment : ".$_POST["comment"];
}
?>
</body>
</html>
Click to Form Validation
0 comments:
Post a Comment
Thanks