|
|||
| Home Blog About | |||
|
|
PHP Form Validation Script
It is very essential to have the input to your form validated before taking the
form submission data for further processing.
When there are many fields in the form, the PHP
validation script becomes too complex. Moreover, since you are doing the
same or similar validation for most of the forms that you make, just too much of
duplicate effort is spent on form validations.
About this generic PHP form validation script
The generic PHP form validator makes it very easy to add validations to your form.
We create and associate a set of "validation descriptors" with each element in the form. The "validation descriptor" is a string specifying the type of validation to be performed. For example, "req" means required, "alpha" means allow only alphabetic characters and so on. Each field in the form can have 0, 1, or more validations. For example, the input should not be empty, should be less than 25 chars, should be alpha-numeric, etc You can associate a set of validation descriptors for each input field in the form.Download the PHP form validation scriptYou can download the PHP form validation script below:php-form-validator.zip The zip file contains the form validation script formvalidator.php, documentation and usage samples. Using the PHP form validation script
$validator = new FormValidator();
$validator->addValidation("Name","req","Please fill in Name");
$validator->addValidation("Email","email",
"The input for Email should be a valid email value");
$validator->addValidation("Email","req","Please fill in Email");
The first argument is the name of the input field in the form. The second argument is the validation descriptor that tells the type of the validation required. The third argument is the error message to be displayed if the validation fails.
if(!$validator->ValidateForm())
{
echo "<B>Validation Errors:</B>";
$error_hash = $validator->GetErrors();
foreach($error_hash as $inpname => $inp_err)
{
echo "<p>$inpname : $inp_err</p>\n";
}
}
Example
The example below will make the idea clearer
<?PHP
require_once "formvalidator.php";
$show_form=true;
if(isset($_POST['Submit']))
{
$validator = new FormValidator();
$validator->addValidation("Name","req","Please fill in Name");
$validator->addValidation("Email","email",
"The input for Email should be a valid email value");
$validator->addValidation("Email","req","Please fill in Email");
if($validator->ValidateForm())
{
echo "<h2>Validation Success!</h2>";
$show_form=false;
}
else
{
echo "<B>Validation Errors:</B>";
$error_hash = $validator->GetErrors();
foreach($error_hash as $inpname => $inp_err)
{
echo "<p>$inpname : $inp_err</p>\n";
}
}
}
if(true == $show_form)
{
?>
<form name='test' method='POST' action='' accept-charset='UTF-8'> Name: <input type='text' name='Name' size='20'> Email: <input type='text' name='Email' size='20'> <input type='submit' name='Submit' value='Submit'> </form> <?PHP }//true == $show_form ?> Adding Custom Validation
If you want to add a custom validation, which is not provided by the validation descriptors,
you can do so. Here are the steps:
class MyValidator extends CustomValidator
{
function DoValidate(&$formars,&$error_hash)
{
if(stristr($formars['Comments'],'http://'))
{
$error_hash['Comments']="No URLs allowed in comments";
return false;
}
return true;
}
}
$validator = new FormValidator();
$validator->addValidation("Name","req","Please fill in Name");
$validator->addValidation("Email","email",
"The input for Email should be a valid email value");
$validator->addValidation("Email","req","Please fill in Email");
$custom_validator = new MyValidator();
$validator->AddCustomValidator($custom_validator);
The custom validation function will be called automatically after other validations. Table of Validation Descriptors
|
| Copyright © 2008-2009 html-form-guide.com . All rights reserved. | ||