In PHP website development and construction, user registration and message are essential functions. information data submitted by users is submitted through Form. to ensure data integrity and security, PHPForm verification is the first step in data filtering. PHP has a variety of verification methods for form submission data. in the early days, Javascript was generally used in PHP website development and construction, user registration and message are essential functions. information data submitted by users is submitted through Form. to ensure data integrity and security, PHP Form verification is the first step in data filtering. PHP verifies the Form submission data in a variety of ways. in the early days, PHP forms were generally verified using Javascript. With the advent of the Web era, considering the user experience, Jquery Form verification plug-in is a common method. today I want to introduce a PHP form verification Class PHP Form validator, which can be used for common PHP form verification, it can also be integrated with third-party programs or Javascript for form verification. you can also customize a specific PHP table. Single verification rules, PHP form validator is very convenient to use.
PHP form validator form verification class instructions
1. Download PHP form validator and decompress it to the website running directory configured in the PHP environment.
2. create a Form to be verified and create the most common options, such as username, Email, and message box. the specific PHP Form verification instance source code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Require_once "formvalidator. php ";
$ Show_form = true;
If (isset ($ _ POST ['submit '])
{
$ Validator = new FormValidator ();
$ Validator-> addValidation ("Name", "req", "enter the user Name ");
$ Validator-> addValidation ("Email", "email", "Please enter the correct Email address ");
$ Validator-> addValidation ("Email", "req", "Please fill in the Email address ");
If ($ validator-> ValidateForm ())
{
Echo "PHP form verification succeeded. thank you for your support for www.leapsoul.cn ";
$ Show_form = false;
}
Else
{
Echo "error prompt :";
$ Error_hash = $ validator-> GetErrors ();
Foreach ($ error_hash as $ inpname => $ inp_err)
{
Echo"
$ Inpname: $ inp_err \ N ";
}
}
}
If (true = $ show_form)
{
?>
} // True = $ show_form
?> |
Explanation of PHP form validator form verification source code example
Row 1st: to enable the PHP form validator form verification class, the first step is to add formvalidator. php require.
Row 3rd: $ show_form is used to control the form Display switch. The form is displayed by default.
6th ~ Row 9: when submitting a form to be verified, first create an object of the PHP form verification class and add the option to be verified in the form, in the code instance, the user Name and Email address are verified.
Three parameters of the addValidation function are described in detail.: The first parameter indicates the project in the form to be verified, that is, the input name. The second parameter indicates the description of the verification rule, such as required and the length of characters. the verification rules are described below; the third parameter indicates the error message to be displayed when the form verification fails.
10th ~ Row 24: Use the ValidateForm () function of the PHP form validator form verification class to determine whether the form has passed verification. if the verification fails, the error message is displayed. this code has a great degree of freedom, you can modify the settings as needed. for example, you can jump to a specific page.
Now, the most basic usage of the PHP form validator form verification class has been introduced. if the form verification rules defined by the PHP form validator do not meet your needs, you can customize the specific verification rules by yourself, the method is as follows:
1. create a subclass that inherits the custom verification class CustomValidator, for example, MyValidator, and override the DoValidate () function, that is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Class MyValidator extends CustomValidator
{
Function DoValidate (& $ formars, & $ error_hash)
{
If (stristr ($ formars ['comments'], 'http ://'))
{
$ Error_hash ['comments'] = "the message content cannot have a URL address ";
Return false;
}
Return true;
}
}
?> |
The preceding code defines a form verification rule, that is, the message content cannot contain URL addresses.
2. add custom form verification rules to existing form verification rules, that is
1
2
3
4
5
6
7
|
$ Validator = new FormValidator ();
$ Validator-> addValidation ("Name", "req", "enter the user Name ");
$ Validator-> addValidation ("Email", "email", "Please enter the correct Email address ");
$ Validator-> addValidation ("Email", "req", "Please fill in the Email address ");
$ Custom_validator = new MyValidator ();
$ Validator-> AddCustomValidator ($ custom_validator ); |
Custom form verification rules are automatically called after other form verification is completed.
So far, the use of custom authentication rules for PHP form validator form verification class is complete, in addition, the PHP form validator form verification class can also be combined with Javascript, third-party JS, PHP class libraries for form verification, which is highly scalable, for more PHP form validator form verification instances, see the sample code in the downloaded file. The following describes the default verification rules of the PHP form validator form verification class.
Verification rules for PHP form validator form verification
ReqRequired
Maxlen = ???Check the maximum length of input data. for example, if the maximum length is 25, a verification description of maxlen = 25 is given.
Minlen = ???Check the minimum length of input data. for example, the minimum length is 5.
AlnumOnly letters and numbers are allowed
Alnum_sOnly letters, numbers, and spaces are allowed.
NumCheck numeric data
AlphaCheck letter data
Alpha_sLetters and spaces are allowed.
EmailValid Email address
Lt = ???
Lessthan = ???Verify whether the data is less than the given value. it is only applicable to numeric fields. for example, if the value is less than 1000, that is, less than 1000
Gt = ???
Greaterthan = ???Verify that the data is greater than the given value and only applies to numeric fields. for example, if the value is greater than 10
Regexp = ???Verify that the data matches the given regular expression
Dontselect = ??Check whether the select option is selected. select is an option in the select option list ". If you cannot select the option "select", set the dontselect value to "select"
DontselectchkThis verification description is for check boxes (check box). you cannot select a set check box.
ShouldselchkYou need to select the set check box
DontselectradioThis verification description is for the radio button (single-choice button). you cannot select the set single-choice button.
SelectradioSelect the specified radio button.
Selmin = ??How many check boxes must be selected in the check box Group, for example, selmin = 3
SeloneYou must select at least one item from the single-choice button group.
Eqelmnt = ???Compare the two elements in the Form and check whether the values are the same, for example, "password" and "confirm password", replace ??? The value is subject to the name of the input element, for example, eqelmnt = confirm_pwd.
OK. the verification rules for the PHP form validator form verification class are described. if you have a poor translation or understanding, try to leave a message or try again.
As long as you demonstrate several verification examples in the PHP form validator form verification class, you will find that the user experience is no worse than form verification plug-ins such as Jquery, custom PHP form verification is also very convenient and practical.