Simply write PHP validation classes to teach you how to write PHP programs (with multiple validation rules) _php Tutorial

Source: Internet
Author: User
Many people in the development of the site is often only through simple JS verification, when you accidentally in the JS more than a comma or the number of points, IE6 can not recognize the direct skip verification. In fact, the safest way to do this is to verify the data entered by the user on the server. I wrote a simple PHP validation class, including a variety of validation rules for everyone to learn the reference. Original link


[PHP]
/**
* User Input Rule validation class
* Author Haining Zhang
* Date 2013-05-23
*/
Class validate{
Validation rules
Private $role _name = Array (
Verify whether it is empty
' Required ',

Match mailbox
' Email ',

Match ID
' Idcode ',

Match numbers
' Number ',

Match HTTP Address
' HTTP ',

Match QQ number
' QQ ',

Match China postcode
' Postcode ',

Match IP Address
' IP ',

Match phone format
' Telephone ',

Match phone format
' Mobile ',

Match 26 Letters of English
' En_word ',

Match only Chinese
' Cn_word ',

Verify accounts (Letters start with alphanumeric underscores, 4-20 bytes)
' User_account ',
);

/**
* [Validation function]
* @param [array] $data [data to be verified by the user]
* @param [array] $validate _role [validation rules]
* @param [array] $validate _err_msg [error message Tip]
* @return [BOOL] [successful return true, Failure returns 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;
}

Get an array of rules
Public Function Get_role_name () {
return $this->role_name;
}

Setting property Rules
Public Function Set_role_name ($arr) {
$this->role_name = Array_merge ($this->role_name, $arr);
}

Verify whether it is empty
Public function required ($STR) {
if (Trim ($str)! = "") return true;
return false;
}

Verify message 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 ID
Public Function Idcode ($STR) {
if (Preg_match ("/^\d{14} (\d{1}|\d{4}| ( \D{3}[XX]) $/", $str)) return true;
else return false;
}

Verifying HTTP Addresses
Public function http ($str) {
if (Preg_match ("/[a-za-z]+:\/\/[^\s]*/", $str)) return true;
else return false;
}

Matching QQ Number (QQ number starting from 10000)
Public Function QQ ($STR) {
if (Preg_match ("/^[1-9][0-9]{4,}$/", $str)) return true;
else return false;
}

Match China postcode
Public function postcode ($STR) {
if (Preg_match ("/^[1-9]\d{5}$/", $str)) return true;
else return false;
}

Match 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 phone format
Public Function Telephone ($STR) {
if (Preg_match ("/^\d{3}-\d{8}$|^\d{4}-\d{7}$/", $str)) return true;
else return false;
}

Match 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 Letters of English
Public Function En_word ($STR) {
if (Preg_match ("/^[a-za-z]+$/", $str)) return true;
else return false;
}

Match only Chinese
Public Function Cn_word ($STR) {
if (Preg_match ("/^[\x80-\xff]+$/", $str)) return true;
else return false;
}

Verify accounts (Letters start with alphanumeric underscores, 4-20 bytes)
Public Function User_account ($STR) {
if (Preg_match ("/^[a-za-z][a-za-z0-9_]{3,19}$/", $str)) return true;
else return false;
}

Verifying numbers
Public function number ($STR) {
if (Preg_match ("/^[0-9]+$/", $str)) return true;
else return false;
}
}

/**
* User Input Rule validation class
* Author Haining Zhang
* Date 2013-05-23
*/
Class validate{
Validation rules
Private $role _name = Array (
Verify whether it is empty
' Required ',

Match mailbox
' Email ',

Match ID
' Idcode ',

Match numbers
' Number ',

Match HTTP Address
' HTTP ',

Match QQ number
' QQ ',

Match China postcode
' Postcode ',

Match IP Address
' IP ',

Match phone format
' Telephone ',

Match phone format
' Mobile ',

Match 26 Letters of English
' En_word ',

Match only Chinese
' Cn_word ',

Verify accounts (Letters start with alphanumeric underscores, 4-20 bytes)
' User_account ',
);

/**
* [Validation function]
* @param [array] $data [data to be validated by user]
* @param [array] $validate _role [validation rule]
* @param [array] $validate _err_msg [error message prompt]
* @return [BOOL] [successful return true, Failure returns error message]
*/
Public Function Verify ($data, $validate _r OLE, $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 =& Gt $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;
}

Get an array of rules
Public Function Get_role_name () {
return $this->role_name;
}

Setting property Rules
Public Function Set_role_name ($arr) {
$this->role_name = Array_merge ($this->role_name, $arr);
}

Verify whether it is empty
Public function required ($STR) {
if (Trim ($str)! = "") return true;
return false;
}

Verify message 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 ID
Public Function Idcode ($STR) {
if (Preg_match ("/^\d{14} (\d{1}|\d{4}| ( \D{3}[XX]) $/", $str)) return true;
else return false;
}

Verifying HTTP Addresses
Public function http ($str) {
if (Preg_match ("/[a-za-z]+:\/\/[^\s]*/", $str)) return true;
else return false;
}

Matching QQ Number (QQ number starting from 10000)
Public Function QQ ($STR) {
if (Preg_match ("/^[1-9][0-9]{4,}$/", $str)) return true;
else return false;
}

Match China postcode
Public function postcode ($STR) {
if (Preg_match ("/^[1-9]\d{5}$/", $str)) return true;
else return false;
}

Match 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 phone format
Public Function Telephone ($STR) {
if (Preg_match ("/^\d{3}-\d{8}$|^\d{4}-\d{7}$/", $str)) return true;
else return false;
}

Match 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 Letters of English
Public Function En_word ($STR) {
if (Preg_match ("/^[a-za-z]+$/", $str)) return true;
else return false;
}

Match only Chinese
Public Function Cn_word ($STR) {
if (Preg_match ("/^[\x80-\xff]+$/", $str)) return true;
else return false;
}

Verify accounts (Letters start with alphanumeric underscores, 4-20 bytes)
Public Function User_account ($STR) {
if (Preg_match ("/^[a-za-z][a-za-z0-9_]{3,19}$/", $str)) return true;
else return false;
}

Verifying numbers
Public function number ($STR) {
if (Preg_match ("/^[0-9]+$/", $str)) return true;
else return false;
}
}
Calling methods


[PHP]
Require (' model/validate.php ');
$data = Array (
"Username" = ' ningofaura@gmail.com ',
"QQ" = ' 593084029 ',
"Nickname" = ' Zhang Haining ',
"id" = ' 24 ',
);
$validate _role = Array (
' Username ' =>array (
' Required ' =>true,
' Email ' =>true,
),
' QQ ' =>array (
' Required ' =>true,
' QQ ' =>true,
),
' Nickname ' =>array (
' Required ' =>true,
),
' ID ' =>array (
' Required ' =>true,
' Number ' =>true,
),
);

$validate _err_msg = Array (
' Username ' =>array (
' Required ' = ' user name cannot be empty ',
' Email ' + ' email format is incorrect ',
),
' QQ ' =>array (
' Required ' = ' QQ cannot be empty ',
' QQ ' = ' QQ format is incorrect ',
),
' Nickname ' =>array (
' Required ' = ' nickname cannot be empty ',
),
' ID ' =>array (
' Required ' = ' id cannot be empty ',
' Number ' = ' is not a digit '
),
);
$Validate = new Validate ();
$rt = $Validate->verify ($data, $validate _role, $validate _err_msg);
if ($rt!== true) {
Echo $rt;
Exit
}

!--? php
require (' model/validate.php ');
$data = Array (
"username" + ' ningofaura@gmail.com ',
"qq" = ' 593084029 ',
"nickname" = "Zhang Haining",
"id", "+",
);
$validate _role = array (
' username ' =>array (
' Required ' =>true,
' email ' =>true,
),
' qq ' = >array (
' Required ' =>true,
' QQ ' =>true,
),
' nickname ' =>array (
' Required ' =>true,
),
' id ' =>array (
' Required ' =>true,
' number ' =>true,
),
);

$validate _err_msg = Array (
' Username ' =>array (
' Required ' = ' user name cannot be empty ',
' Email ' + ' email format is incorrect ',
),
' QQ ' =>array (
' Required ' = ' QQ cannot be empty ',
' QQ ' = ' QQ format is incorrect ',
),
' Nickname ' =>array (
' Required ' = ' nickname cannot be empty ',
),
' ID ' =>array (
' Required ' = ' id cannot be empty ',
' Number ' = ' is not a digit '
),
);
$Validate = new Validate ();
$rt = $Validate->verify ($data, $validate _role, $validate _err_msg);
if ($rt!== true) {
Echo $rt;
Exit
}

Of course, if you feel that validation doesn't meet your needs, you can also create subclasses to extend your approach


[PHP]
/**
* User Database Asynchronous authentication
* Author Haining Zhang
* Date 2013-05-23
*/
Class Ajaxvalidate extends validate{
Private $role _name = Array (
Verify that the user name exists
' Is_username ',

Verify that the nickname is present
' Is_nickname ',
);

Private $db;

Public Function __construct () {
$this->db = &load_system ("Database");
$this->set_role_name ($this->role_name);
}

Determines whether the user name can be registered (prevents duplicate 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 if a nickname is available (prevent nickname duplication)
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;
}
}
}

/**
* User Database Asynchronous authentication
* Author Haining Zhang
* Date 2013-05-23
*/
Class Ajaxvalidate extends validate{
Private $role _name = Array (
Verify that the user name exists
' Is_username ',

Verify that the nickname is present
' Is_nickname ',
);

Private $db;

Public Function __construct () {
$this->db = &load_system ("Database");
$this->set_role_name ($this->role_name);
}

Determines whether the user name can be registered (prevents duplicate 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 if a nickname is available (prevent nickname duplication)
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;
}
}
}

http://www.bkjia.com/PHPjc/477434.html www.bkjia.com true http://www.bkjia.com/PHPjc/477434.html techarticle many people in the development of the site is often only through simple JS verification, when you accidentally in the JS more than a comma or the number of points, IE6 can not recognize the direct skip verification. In fact, the most Ann ...

  • 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.