PHP form form verification: PHP Form Validator Usage Instructions

Source: Internet
Author: User
Tags comments configuration settings php class php form php form validation php web development valid email address validation examples

In the development and construction of PHP Web site, user registration, message is an essential function, the user submitted information data are submitted through form form, in order to ensure the integrity of the data, security, PHP form form verification is the first link to filter data, PHP to form the data submitted to verify the various ways, Early in the general use of JavaScript to verify PHP forms, with the advent of the WEB2.0 era, taking into account the user experience, using the jquery form verification plug-in is a common technique, today I want to introduce a PHP form verification class php form validator, It can be applied to general PHP form verification, but also with third-party programs or JavaScript integration to form verification, you can customize the specific PHP form validation rules, PHP form validator easy to use.

  PHP Form Validator Form validation class usage instructions

1, download the php form validator, and extract to the PHP environment configuration settings of the Web site running directory

2, create a form to be validated, create the most commonly used options, such as user name, Email, message box, specific PHP form verification instance source code as follows





































"border:0;padding:0" ><?php require_once Formvalidator.php "; $show _form=true; if (isset ($_post[' Submit ')) {     $validator = new Formvalidator ();     $validator-& Gt;addvalidation ("name", "req", "Please fill in username");     $validator->addvalidation ("email", "email", "please fill in the correct email address");     $validator->addvalidation ("email", "req", "Please fill in the email address");     if ($validator->validateform ())     {         echo "PHP form verification success, Xie Thank you for your support of www.leapsoul.cn ";         $show _form=false;    }     Else     {        -echo "errorFalse hint: ";           $error _hash = $validator->geterrors ()         Fore Ach ($error _hash as $inpname => $inp _err)         {           Echo "<p> $inpname: $inp _err</p>\n";        }    } }   if (true = = $show _form) {] <form name= ' test ' method= ' POST ' action= ' accept-chars et= ' UTF-8 ' Name: <input type= ' text ' name= ' name ' size= ' Email: <input type= ' text ' name= ' email ' Size= ' <input type= ' submit ' name= ' submit ' value= ' Submit ' </form> <?php }//true = = $show _form
1
2
3
4
5
6
7
8
9
Ten
One

to




style=

  PHP Form Validator Form Verification Source example interpretation

Line 1th: To enable the PHP form Validator Form validation class, first step requires the formvalidator.php require to come in.

Line 3rd: $show _form is the switch that controls the display of the form, and the default display form.

Line 6th to 9th: When submitting a form that needs to be validated for processing, first create an entity for the PHP form validation class and add the options that are required for validation in the form, primarily validating the user name (name) and email address two options in the code instance.

  the three parameters of the Addvalidation function specify that the first parameter represents the item in the form that needs to be validated, that is, input name; the second parameter represents the rule description of the validation, such as mandatory fill, character length, and so on, and the various validation rules are described below The third parameter represents the error message that needs to be displayed when the form validation is not passed.

Line 10th to 24th: Through the php form Validator Form Validation Class Validateform () function to determine whether the form is validated, if not through validation, display error message, this code is very free, you can modify as needed, For example, jump to a specific page can be achieved.

So far, the most basic method of using the PHP form Validator form validation is described, and if the form validation rules that you define do not meet your needs, you can customize the specific validation rules by customizing the following methods

1. Create a subclass that inherits the custom validation class CustomValidator, such as Myvalidator, and rewrite the Dovalidate () function, which is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
Class Myvalidator extends CustomValidator
{
Function dovalidate (& $formars,& $error _hash)
{
if (Stristr ($formars [' Comments '], ' http://'))
{
$error _hash[' Comments ']= "message content can not have URL address";
return false;
}
return true;
}
}
?>

A form validation rule is customized in the preceding code, that is, the message content cannot contain a URL address.

2. Add a custom form validation rule to an existing form validation rule, that is,

1
2
3
4
5
6
7
$validator = new Formvalidator ();
$validator->addvalidation ("name", "req", "Please fill in username");
$validator->addvalidation ("email", "email", "please fill in the correct email address");
$validator->addvalidation ("email", "req", "Please fill in the email address");

$custom _validator = new Myvalidator ();
$validator->addcustomvalidator ($custom _validator);

Custom form validation rules are invoked automatically when other forms are validated.

So far, the PHP form validator The custom validation rules of the class to use the method is introduced, in addition to PHP form Validator Form Validation classes can also be combined with JavaScript or Third-party JS, PHP Class Library for form verification, scalability is very strong, More php form Validator form validation instances refer to the sample code in the download file. The following describes the default validation rules for PHP form validator Form Validation classes

  PHP Form Validator Validation Rule description for a form validation class

  req must fill
  maxlen=??? Check the maximum length of the input data, such as allowing a maximum length of 25, gives the MAXLEN=25 verification description
  minlen=??? Check the minimum length of the input data, such as allowing a minimum length of 5
  alnum only allows letters and numbers
  alnum_s only allows letters, numbers, and spaces
  num Check digital data
  Alpha Check for alphabetic data
  alpha_s allow letters and spaces
   Email a valid email address

  Lt=???
  lessthan=??? Verifies that the data is less than the given value and applies only to numeric fields, for example, if the value is less than 1000, that is, the lt=1000

  Gt=???
  greaterthan=??? Verifies that the data is greater than the given value and applies only to numeric fields, for example, if the value is greater than 10, that is, lt=10

  regexp=??? Verifies that the data matches a given regular expression
  dontselect=?? Verify that the Select option is selected, and one option in the Select option list is "Please select". If the option you cannot select is "Please select", you need to set the Dontselect value to "please select"
  Dontselectchk This validation description is for check boxes (check box), the user cannot select a set check box
  shouldselchk users need to select a set of check boxes
  Dontselectradio This validation description is for the radio button (radio buttons), the user cannot select the Set radio button
  Selectradio users need to select a given radio button
  selmin=?? check box Group at least how many check boxes to select, such as Selmin=3
  Selone users need to select at least one item from the radio button group
  eqelmnt=??? Compare the two elements in the form form and confirm that the values are the same, such as "Password" (password) and "Confirm Password" (Confirm password), instead of??? Value is based on the name of the input element, such as Eqelmnt=confirm_pwd

ok,php Form Validator Validation Rule description of the verification class is finished, translation is not good or understand the problem may be more hands-on or message.

As long as you demonstrate several validation examples in the php form Validator Form Validation class, you will find that the user experience is never worse than a form validation plug-in such as jquery, and custom PHP form validation is also very handy and practical.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.