SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

PHP Advance Form validation code

PHP form validation is a frequent requirement in many applications. Different type of php form validations is used based on the requirements but the process remains same. Let us start with a simple one and then go to the complex email validation using PHP regular expression.

Let us keep an input box and we will not allow any user to enter null data in it. So our form validation script will check for null entry and do the processing accordingly. Our main page where form is there is called form.php and let us posts the data to another page and name it as  form_validate.php. We will check and validate the variables in form_validate.php page. Here is the simple html form. 

<form method=post action=form_validate.php>
<input type=text name=userid>
<input type=text name=password>
<input type=submit value=Submit></form>

On submit of this form all entered variables will be available on form_validate.php page so let us go to form_validate.php page and read the variable and check if any value is there or not. Let us use one flag to check the status of the variables for form validation.

<?php

$flag="OK";   // This is the flag and we set it to OK
$msg="";        // Initializing the message to hold the error messages

if(strlen($userid) < 5){    // checking the length of the entered userid and it must be more than 5 character in length
$msg=$msg."( Please enter user id more than 5 character length  )<BR>";
$flag="NOTOK";   //setting the flag to error flag.
}

User id validation is over, now let us start password checking.

if(strlen($password) < 5 ){    // checking the length of the entered password and it must be more than 5 character in length
$msg=$msg."( Please enter password of more than 5 character length  )<BR>";
$flag="NOTOK";   //setting the flag to error flag.
}

Now let us check the flag and give message accordingly, if the flag is set to OK then all validations is passed and we can proceed for the database checking. If the flag is set to NOTOK then entries are not ok so we have to display the messages.  We also give one back button for the user to go back and correct the entries.

if($flag <>"OK"){
echo "<center>$msg <br>
<input type='button' value='Retry' onClick='history.go(-1)'></center>";
}else{ // all entries are correct and let us proceed with the database checking etc …
}
 

This is PHP form validation by using two simple text boxes. We have checked only the number of characters entered by the user. Now let us move one more step and go for form validation checking the type of entry. Say in userid field  other than   characters are not allowed. We will use same flag to set the status and append the message to the message variable.

if(ereg('[^A-Za-z]', $userid)){    //Only lower or upper case letters allowed.
$msg=$msg."( Please use only alphabets a to z as userid   )<BR>";
$flag="NOTOK";   //setting the flag to error flag.
}

ereg() is a PHP regular expression function and this can be used to get powerful email form validation. We will use this function more for advance PHP form validation



Further readings
All form components validation with post back and data locking
Checkbox
How to retain form data if validation fails?
How to retain data of a text box after submitting
Retaining status of a check box
Updating checkbox value from & to a table record.
Retaining selected data of a period button of a form
Retaining selected data of a drop down list box of a form
Handling drop down list box with multiple selection options using array
Validating Date: Checking if date exist
Email address validation in a form
Email validation using Ajax & PHP
PHP Form Validation
is_numeric to check numeric numbers
ctype_alnum to check alphanumeric characters data
ctype_alpha to check alphabetic characters data
How to take care of form & query string variables if Register global is OFF?
















Join Our Email List
Email:  
For Email Newsletters you can trust
Form handling
PHP Sections