Hallo Guys
Now i am showing you how to validate variables in PHP.
ins this post we also discuss PHP INBUILD FUNCTION for email and phone no. validation.
# Get values from user & print in front
<?php
$name = ' '; //by default we declare variables none
$age = ' '; //by default we declare variables none
$password = ''; //by default we declare variables none
$mail = ' '; //by default we declare variables none
$phone= ' '; //by default we declare variables none
$doj = ' '; //by default we declare variables none
$pass= ' '; //by default we declare variables none
$error = ' '; //by default we declare variables none
// check if user press the submit button then follow
if(isset($_POST['submit']))
{
$name=$_POST['uname'];
$age=$_POST['age'];
$password=$_POST['password'];
$pass=strlen($password);
$mail=$_POST['mail'];
$phone=$_POST['phone'];
$doj=$_POST['doj'];
// check field value is empty or not
// if empty then store error in error variabl
if(empty($name))
{
$error="please enter the user name";
}
else if(empty($age))
{
$error="please enter the age";
}
else if(empty($password))
{
$error="please enter the password";
}
else if($pass<=6)
{
$error="password must be more then 6 character";
}
else if(empty($mail))
{
$error="please enter the mail";
}
else if(!filter_var($mail, FILTER_VALIDATE_EMAIL)) // check email validation by php inbuild function
{
$error="enter a valid email";
}
else if(empty($phone))
{
$error="please enter the phone no.";
}
else if(empty($doj))
{
$error="please enter the date of Joining.";
}
}
?>
<table width="30%" align="center" border="1px">
<tr bgcolor="#990000">
<th style="color:#FFFFFF">Field</th><th style="color:#FFFFFF">Input Values</th>
</tr>
<tr>
<td>Name : </td><td><?php echo $name; ?></td>
</tr>
<tr>
<td>Age : </td><td><?php echo $age; ?></td>
</tr>
<tr>
<td>Password : </td><td><?php echo $password; ?></td>
</tr>
<tr>
<td>Mail : </td><td><?php echo $mail; ?></td>
</tr>
<tr>
<td>Phone : </td><td><?php echo $phone; ?></td>
</tr>
<tr>
<td>Date of Joining : </td><td><?php echo $doj; ?></td>
</tr>
</table>
<br />
<!-- Display error message if found -->
<p style="color:red;">
<?php
if(isset($error))
{
echo $error;
}?>
</p>
<!-- we empty form action because we display values same page --->
<form action="" method="post">
<table align="center" width="45%" border="1px">
<tr bgcolor="#990000">
<th style="color:#FFFFFF">Field</th><th style="color:#FFFFFF">Input Values</th>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="Name" /></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" value="Age" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" value="Password" /></td>
</tr>
<tr>
<td>Mail</td>
<td><input type="text" name="mail" value="Email" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" value="Phone" /></td>
</tr>
<tr>
<td>Date of Joining</td>
<td><input type="date" name="doj" value="Date of Joining" /></td>
</tr>
<tr>
<td colspan="2" style="padding-left:150px;"><input type="submit" name="submit" value="Show" /> <input type="reset" name="reset" value="Clear" /></td>
</tr>
</table>
</form>

