PHP Advance Form validation codePHP 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.
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.
User id validation is over, now let us start password checking.
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.
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.
ctype_aplha() checks the input here and read more on email validation here. We will use this function more for advance PHP form validation
We can expect user inputs to be of a particular type or format. So we can check the variables to match the format before using them. Here is some sample codes to match required format before using.
This article is written by plus2net.com team.
| ▼ Web Forms & handling data in PHP