A simple php verification class will teach you how to write a php program (including multiple verification rules) _ PHP Tutorial

Source: Internet
Author: User
A simple php verification class teaches you how to write a php program (including multiple verification rules ). When developing a website, many people tend to simply pass JavaScript verification. when you accidentally write a comma or dot in JavaScript, ie6 will skip verification if it cannot be identified. In fact, most people often use simple js verification when developing websites. when you accidentally write a comma or dot in js, if ie6 cannot be identified, the verification is skipped. In fact, the safest way is to verify the user input data on the server. I wrote a simple php verification class, which contains multiple verification rules for your reference. Original article link


[Php]
/**
* User input rule verification class
* Author HaiNing Zhang
* Date 2013-05-23
*/
Class Validate {
// Verification rules
Private $ role_name = array (
// Verify whether it is null
'Requestred ',

// Match the email address
'Email ',

// Match the ID card
'Idcode ',

// Match numbers
'Number ',

// Match the http address
'Http ',

// Match the QQ number
'QQ ',

// Match the Chinese postal code
'Postcode ',

// Match the IP address
'IP ',

// Match the phone format
'Telphone ',

// Match the mobile phone format
'Mobile ',

// Match 26 English letters
'En _ word ',

// Match only Chinese characters
'CN _ word ',

// Verify the account (starting with a letter, consisting of letters, numbers, and underscores (_), 4-20 bytes)
'User _ account ',
);

/**
* [Verify the function]
* @ Param [array] $ data [data to be verified by the user]
* @ Param [array] $ validate_role [verification rules]
* @ Param [array] $ validate_err_msg [error message prompt]
* @ Return [bool] [success returns true, failure returns an error message]
*/
Public function verify ($ data, $ validate_role, $ validate_err_msg = ''){
If (empty ($ data) return false;
If (empty ($ validate_role) return false;
Foreach ($ data as $ key => $ value ){
$ Key = strtolower ($ key );
Foreach ($ validate_role as $ kk => $ vv ){
$ Kk = strtolower ($ kk );
If ($ key = $ kk ){
Foreach ($ vv as $ k => $ v ){
$ K = strtolower ($ k );
If (! In_array ($ k, $ this-> role_name) return 'role name "'. $ k.'" is not found! ';
If ($ v = true ){
If (! $ This-> $ k ($ value )){
If (! Isset ($ validate_err_msg [$ kk] [$ k])
Return 'Var '. $ key. 'in'. $ k.' of regular validation failure! ';
Return $ validate_err_msg [$ kk] [$ k];
}
}
}
}
}
}
Return true;
}

// Obtain the rule array
Public function get_role_name (){
Return $ this-> role_name;
}

// Set property rules
Public function set_role_name ($ arr ){
$ This-> role_name = array_merge ($ this-> role_name, $ arr );
}

// Verify whether it is null
Public function required ($ str ){
If (trim ($ str )! = "") Return true;
Return false;
}

// Verify the email format
Public function email ($ str ){
If (preg_match ("/^ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + \. [a-zA-Z] {2, 3} $/", $ str) return true;
Else return false;
}

// Verify the ID card
Public function idcode ($ str ){
If (preg_match ("/^ \ d {14} (\ d {1} | \ d {4} | (\ d {3} [xX]) $ /", $ str) return true;
Else return false;
}

// Verify the http address
Public function http ($ str ){
If (preg_match ("/[a-zA-Z] +: \/[^ \ s] */", $ str) return true;
Else return false;
}

// Match the QQ number (the QQ number starts from 10000)
Public function qq ($ str ){
If (preg_match ("/^ [1-9] [0-9] {4,} $/", $ str) return true;
Else return false;
}

// Match the Chinese postal code
Public function postcode ($ str ){
If (preg_match ("/^ [1-9] \ d {5} $/", $ str) return true;
Else return false;
}

// Match the IP address
Public function ip ($ str ){
If (preg_match ("/^ \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3} $/", $ str) return true;
Else return false;
}

// Match the phone format
Public function telephone ($ str ){
If (preg_match ("/^ \ d {3}-\ d {8 }$ | ^ \ d {4}-\ d {7} $/", $ str )) return true;
Else return false;
}

// Match the mobile phone format
Public function mobile ($ str ){
If (preg_match ("/^ (13 [0-9] | 15 [0-9] | 18 [0-9]) \ d {8} $ /", $ str) return true;
Else return false;
}

// Match 26 English letters
Public function en_word ($ str ){
If (preg_match ("/^ [A-Za-z] + $/", $ str) return true;
Else return false;
}

// Match only Chinese characters
Public function cn_word ($ str ){
If (preg_match ("/^ [\ x80-\ xff] + $/", $ str) return true;
Else return false;
}

// Verify the account (starting with a letter, consisting of letters, numbers, and underscores (_), 4-20 bytes)
Public function user_account ($ str ){
If (preg_match ("/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/", $ str) return true;
Else return false;
}

// Verify the number
Public function number ($ str ){
If (preg_match ("/^ [0-9] + $/", $ str) return true;
Else return false;
}
}

/**
* User input rule verification class
* Author HaiNing Zhang
* Date 2013-05-23
*/
Class Validate {
// Verification rules
Private $ role_name = array (
// Verify whether it is null
'Requestred ',

// Match the email address
'Email ',

// Match the ID card
'Idcode ',

// Match numbers
'Number ',

// Match the http address
'Http ',

// Match the QQ number
'QQ ',

// Match the Chinese postal code
'Postcode ',

// Match the IP address
'IP ',

// Match the phone format
'Telphone ',

// Match the mobile phone format
'Mobile ',

// Match 26 English letters
'En _ word ',

// Match only Chinese characters
'CN _ word ',

// Verify the account (starting with a letter, consisting of letters, numbers, and underscores (_), 4-20 bytes)
'User _ account ',
);

/**
* [Verify the function]
* @ Param [array] $ data [data to be verified by the user]
* @ Param [array] $ validate_role [verification rules]
* @ Param [array] $ validate_err_msg [error message prompt]
* @ Return [bool] [success returns true, failure returns an error message]
*/
Public function verify ($ data, $ validate_role, $ validate_err_msg = ''){
If (empty ($ data) return false;
If (empty ($ validate_role) return false;
Foreach ($ data as $ key => $ value ){
$ Key = strtolower ($ key );
Foreach ($ validate_role as $ kk => $ vv ){
$ Kk = strtolower ($ kk );
If ($ key = $ kk ){
Foreach ($ vv as $ k => $ v ){
$ K = strtolower ($ k );
If (! In_array ($ k, $ this-> role_name) return 'role name "'. $ k.'" is not found! ';
If ($ v = true ){
If (! $ This-> $ k ($ value )){
If (! Isset ($ validate_err_msg [$ kk] [$ k])
Return 'Var '. $ key. 'in'. $ k.' of regular validation failure! ';
Return $ validate_err_msg [$ kk] [$ k];
}
}
}
}
}
}
Return true;
}

// Obtain the rule array
Public function get_role_name (){
Return $ this-> role_name;
}

// Set property rules
Public function set_role_name ($ arr ){
$ This-> role_name = array_merge ($ this-> role_name, $ arr );
}

// Verify whether it is null
Public function required ($ str ){
If (trim ($ str )! = "") Return true;
Return false;
}

// Verify the email format
Public function email ($ str ){
If (preg_match ("/^ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + \. [a-zA-Z] {2, 3} $/", $ str) return true;
Else return false;
}

// Verify the ID card
Public function idcode ($ str ){
If (preg_match ("/^ \ d {14} (\ d {1} | \ d {4} | (\ d {3} [xX]) $ /", $ str) return true;
Else return false;
}

// Verify the http address
Public function http ($ str ){
If (preg_match ("/[a-zA-Z] +: \/[^ \ s] */", $ str) return true;
Else return false;
}

// Match the QQ number (the QQ number starts from 10000)
Public function qq ($ str ){
If (preg_match ("/^ [1-9] [0-9] {4,} $/", $ str) return true;
Else return false;
}

// Match the Chinese postal code
Public function postcode ($ str ){
If (preg_match ("/^ [1-9] \ d {5} $/", $ str) return true;
Else return false;
}

// Match the IP address
Public function ip ($ str ){
If (preg_match ("/^ \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3} $/", $ str) return true;
Else return false;
}

// Match the phone format
Public function telephone ($ str ){
If (preg_match ("/^ \ d {3}-\ d {8 }$ | ^ \ d {4}-\ d {7} $/", $ str )) return true;
Else return false;
}

// Match the mobile phone format
Public function mobile ($ str ){
If (preg_match ("/^ (13 [0-9] | 15 [0-9] | 18 [0-9]) \ d {8} $ /", $ str) return true;
Else return false;
}

// Match 26 English letters
Public function en_word ($ str ){
If (preg_match ("/^ [A-Za-z] + $/", $ str) return true;
Else return false;
}

// Match only Chinese characters
Public function cn_word ($ str ){
If (preg_match ("/^ [\ x80-\ xff] + $/", $ str) return true;
Else return false;
}

// Verify the account (starting with a letter, consisting of letters, numbers, and underscores (_), 4-20 bytes)
Public function user_account ($ str ){
If (preg_match ("/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/", $ str) return true;
Else return false;
}

// Verify the number
Public function number ($ str ){
If (preg_match ("/^ [0-9] + $/", $ str) return true;
Else return false;
}
}
Call method


[Php]
Require ('Model/Validate. php ');
$ Data = array (
"Username" => 'ningofaura @ gmail.com ',
"Qq" => '000000 ',
"Nickname" => 'Zhang Haining ',
"Id" => '24 ',
);
$ Validate_role = array (
'Username' => array (
'Requestred' => true,
'Email '=> true,
),
'QQ' => array (
'Requestred' => true,
'QQ' => true,
),
'Nickname' => array (
'Requestred' => true,
),
'Id' => array (
'Requestred' => true,
'Number' => true,
),
);

$ Validate_err_msg = array (
'Username' => array (
'Required' => "the user name cannot be blank ",
'Email '=> "incorrect email format ",
),
'QQ' => array (
'Required' => "qq cannot be blank ",
'QQ' => "incorrect qq format ",
),
'Nickname' => array (
'Required' => "nickname cannot be blank ",
),
'Id' => array (
'Required' => "id cannot be blank ",
'Number' => "not a number ",
),
);
$ Validate = new Validate ();
$ Rt = $ Validate-> verify ($ data, $ validate_role, $ validate_err_msg );
If ($ rt! = True ){
Echo $ rt;
Exit;
}

Require ('Model/Validate. php ');
$ Data = array (
"Username" => 'ningofaura @ gmail.com ',
"Qq" => '000000 ',
"Nickname" => 'Zhang Haining ',
"Id" => '24 ',
);
$ Validate_role = array (
'Username' => array (
'Requestred' => true,
'Email '=> true,
),
'QQ' => array (
'Requestred' => true,
'QQ' => true,
),
'Nickname' => array (
'Requestred' => true,
),
'Id' => array (
'Requestred' => true,
'Number' => true,
),
);

$ Validate_err_msg = array (
'Username' => array (
'Required' => "the user name cannot be blank ",
'Email '=> "incorrect email format ",
),
'QQ' => array (
'Required' => "qq cannot be blank ",
'QQ' => "incorrect qq format ",
),
'Nickname' => array (
'Required' => "nickname cannot be blank ",
),
'Id' => array (
'Required' => "id cannot be blank ",
'Number' => "not a number ",
),
);
$ Validate = new Validate ();
$ Rt = $ Validate-> verify ($ data, $ validate_role, $ validate_err_msg );
If ($ rt! = True ){
Echo $ rt;
Exit;
}

Of course, if you think that verification cannot meet your needs, you can create subclass extension methods.


[Php]
/**
* Asynchronous database verification
* Author HaiNing Zhang
* Date 2013-05-23
*/
Class AjaxValidate extends Validate {
Private $ role_name = array (
// Verify whether the user name exists
'Is _ username ',

// Verify whether the nickname exists
'Is _ nickname ',
);

Private $ db;

Public function _ construct (){
$ This-> db = & load_system ("Database ");
$ This-> set_role_name ($ this-> role_name );
}

// Determine whether the user name can be registered (to prevent repeated user names)
Public function is_username ($ username ){
$ _ Username = $ this-> db-> filter ('s ', $ username );
$ SQL = "select id from user where username =". $ _ username;
If ($ this-> db-> num_rows ($ SQL )){
Return false;
} Else {
Return true;
}
}

// Determine whether a nickname is available (to prevent duplicate nicknames)
Public function is_nickname ($ nickname ){
$ _ Nickname = $ this-> db-> filter ('s ', $ nickname );
$ SQL = "select id from user where nickname =". $ _ nickname;
If ($ this-> db-> num_rows ($ SQL )){
Return false;
} Else {
Return true;
}
}
}

/**
* Asynchronous database verification
* Author HaiNing Zhang
* Date 2013-05-23
*/
Class AjaxValidate extends Validate {
Private $ role_name = array (
// Verify whether the user name exists
'Is _ username ',

// Verify whether the nickname exists
'Is _ nickname ',
);

Private $ db;

Public function _ construct (){
$ This-> db = & load_system ("Database ");
$ This-> set_role_name ($ this-> role_name );
}

// Determine whether the user name can be registered (to prevent repeated user names)
Public function is_username ($ username ){
$ _ Username = $ this-> db-> filter ('s ', $ username );
$ SQL = "select id from user where username =". $ _ username;
If ($ this-> db-> num_rows ($ SQL )){
Return false;
} Else {
Return true;
}
}

// Determine whether a nickname is available (to prevent duplicate nicknames)
Public function is_nickname ($ nickname ){
$ _ Nickname = $ this-> db-> filter ('s ', $ nickname );
$ SQL = "select id from user where nickname =". $ _ nickname;
If ($ this-> db-> num_rows ($ SQL )){
Return false;
} Else {
Return true;
}
}
}

Bytes. Actually the most secure...

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.