$fname=trim($_POST['fname']);
$lname=$_POST['lname'];
$email=trim($_POST['email']);
$age=trim($_POST['age']);
$date_user=trim($_POST['date_user']);
$username=trim($_POST['username']);
$age_len=trim(strlen($age));
if(empty($fname))
{
header("location:form.php?msg=pleaseenteremail");
}
//Now email validation using Regular Expressions
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
header("location:form.php?msg=emailnotvalid");
}
//Now performing numeric data validation for Age Field
if(!is_numeric($age))
{
header("location:form.php?msg=agenotvalid");
}
//Now checking the length of the data provided by user as his/her age
if($age_len>3)
{
header("location:form.php?msg=agenotvalid");
}
//Now performing Date Validation for Date Field
$arr=split("/",$date_user); // splitting the array
$mm=$arr[0]; // first element of the array is month
$dd=$arr[1]; // second element is date
$yy=$arr[2]; // third element is year
if(!checkdate($mm,$dd,$yy)){
header("location:form.php?msg=datenotvalid");
}
?>
trim() function is used to remove the spaces before and after the data.
empty() function is a Boolean function that checks whether the data in the form of its argument is empty or not?
is_numeric() function is also a Boolean function that returns whether the data in the form of its argument is numeric or not?
strlen() function provides the length of the string.
checkdate() function is a built in function that checks whether date provided is valid or not. It also takes care of leap year checking. It is also a Boolean function and return true if date is correct and return false if date is false.
Now let's understand the code that is performing validation of form data.
First of all I am getting form data using $_POST because form data coming from previous page is using the post method. Then i remove extra spaces to the form data by using trim() and assign that data to the different variables.
$age_len=trim(strlen($age));
for checking the length of age data, i first of all remove the extra spaces before/after the age data provided by the user and then check the length of age data using strlen() function.
if(empty($fname))
{
header("location:form.php?msg=pleaseenteremail");
}
Then i check whether the first name is empty or not. If empty then returns to the prevous page with a query string that indicates the error.
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
header("location:form.php?msg=emailnotvalid");
}
To validate email address, i use regular expression. Again if email is not a valid email then I redirect page to the previous page with a query string.
if(!is_numeric($age))
{
header("location:form.php?msg=agenotvalid");
}
To check whether age provided is numeric or not i use the is_numeric() function because age can be numeric like 23 not the characters abc.
To perform date validation i use the built in php Boolean function which is checkdate() that takes three arguments which is month, day and year. Now its up to you whether you provide date like this way 02/10/09 or like this way 02/10/2009, this powerful built in function can validate both form of dates. To get month, day and year from the date provided by the user I use split() function to split the date and get the month, day and year and then provide these three to checkdate() function to validate the date.
So this is a practical example to perform form validation in php.
0 comments:
Post a Comment