Enter Keyword Hear for Search This Blog

Monday, 2 May 2016

How to create Regular expression validation in php

index.php

<?php session_start(); ?>
<html>
<head>
<title>Registration</title>
</head>
<body>
<?php
if(isset($_SESSION['vali']['ok']))
{
echo '<span style="color:green;">'.$_SESSION['vali']['ok'].'</span>';
}
?>
<form action="process.php" method="post" enctype="multipart/form-data">
FullName : <br/>
<input type="text" name="fnm">
<?php 
if(isset($_SESSION['vali']['fnm']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['fnm'].'</span>';
}
?>
<br/><br/>

UserName : <br/>
<input type="text" name="unm">
<?php 
if(isset($_SESSION['vali']['unm']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['unm'].'</span>';
}
?>
<br/><br/>
Password : <br/>
<input type="password" name="pwd">
<?php 
if(isset($_SESSION['vali']['pwd']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['pwd'].'</span>';
}
?>
<br/><br/>
Conform Password : <br/>
<input type="password" name="cpwd">
<?php
if(isset($_SESSION['vali']['cpwd']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['cpwd'].'</span>';
}
if(isset($_SESSION['vali']['pwdm']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['pwdm'].'</span>';
}
?>
<br/><br/>
Mobile No : <br/>
<input type="text" name="mno">
<?php 
if(isset($_SESSION['vali']['mno']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['mno'].'</span>';
}
if(isset($_SESSION['vali']['digits']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['digits'].'</span>';
}
if(isset($_SESSION['vali']['max']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['max'].'</span>';
}
?>
<br/><br/>

Email Id : <br/>
<input type="text" name="email">
<?php 
if(isset($_SESSION['vali']['email']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['email'].'</span>';
}
if(isset($_SESSION['vali']['regex']))
{
echo '<span style="color:red;">'.$_SESSION['vali']['regex'].'</span>';
}
unset($_SESSION['vali']);
?>
<br/><br/>
<input type="submit" value="Submit">

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


process.php

<?php  session_start();
$_SESSION['vali']=array();
extract($_POST);

if(empty($_POST['fnm']))
{
$_SESSION['vali']['fnm']="Enter Fullname";
}
if(empty($_POST['unm']))
{
$_SESSION['vali']['unm']="Enter Username";
}
if(empty($_POST['pwd']))
{
$_SESSION['vali']['pwd']="Enter Password";
}
if(empty($_POST['cpwd']))
{
$_SESSION['vali']['cpwd']="Enter Re-Password";
}
if($_POST['pwd'] != $_POST['cpwd'])
{
$_SESSION['vali']['pwdm']="Do not match password";
}
if(empty($mno))
{
$_SESSION['vali']['mno']="Enter Mobile No";
}
else if(!is_numeric($mno))
{
$_SESSION['vali']['digits']="Enter Digits";
}
else if(strlen($_POST['mno'])<10)
{
$_SESSION['vali']['max']="Enter 10 Digits";
}
if(empty($_POST['email']))
{
$_SESSION['vali']['email']="Enter Email Id";
}
if(! ereg("^[a-z0-9_]+[a-z0-9_.]*@[a-z0-9_-]+[a-z0-9.-]*\.[a-z]{2,5}$",$_POST['email']))
{
$_SESSION['vali']['regex']="Email not valid";
}
if(!empty($_SESSION['vali']))
{
header('location:index.php');
}
else
{

extract($_POST);

$link=mysql_connect('localhost','root','')or die("connect problem");
mysql_select_db('test',$link);
$q="insert into register(fnm,unm,pwd,mno,email)values('$fnm','$unm','$pwd','$mno','$email')";
mysql_query($q);
$_SESSION['vali']['ok']="Record Insrted";
header('location:index.php');

}
?>

No comments:

Featured post

what is ajax?how to create ajax with php code

Ajax Introduction -  એજેક્સ નો ઉપીયોગ કરી ને તમે પેજ લોડ કાર્ય વિના બધી માહિતી મેળવી શકો.એજેક્સ થી તમે કોઈ પણ ડેટા ઝડપ થી મેળવી શકો છો...