PHP usage validation class and regular _php tutorial

Source: Internet
Author: User
PHP Common validation class and regular

Regular expressions are constantly updated when they encounter new

Include "validateparameterconfig.php";

Class Validation

{

private static function Getrexp ($REXP)

{

$_rexp = Array (

' Letter_number ' = '/^[0-9a-za-z]+$/',//Only alphanumeric includes case

' Account ' = '/^[0-9a-za-z_]+$/',//Only alphanumeric underscores include case

' IDs ' = '/^[0-9]+ (\,[0-9]+) *$/',//Verify multiple IDs with ', ' split types such as ' 1,2,3,4,5 '

' Number ' = '/^[0-9]+$/',//can only make the numbers

' Personal_card ' = '/^[0-9a-za-z]+$/',//ID card

' Email ' = '/^ ([a-za-z0-9_\.\-]) +\@ ([a-za-z0-9\-]) +\.) + ([a-za-z0-9]{2,4}) +$/',//mailbox

' Date ' = '/^ ((((19|20) \d{2})-(0? ( 1| [3-9]) | 1[012])-(0?[ 1-9]| [12]\d|30)] | (((19|20) \d{2})-(0?[ 13578]|1[02])-31) | ((19|20) \d{2}) -0?2-(0?[ 1-9]|1\D|2[0-8]) | (((19|20) ([13579][26]|[ 2468][048]|0[48]) | (2000)) -0?2-29)) $/'//date, including leap year

);

if (Isset ($_rexp[$rexp])) {

return $_rexp[$rexp];

} else {

return $rexp _not_defind;

}

}

public static function Validate ($data, $config)

{

$_config = Validateparameterconfig::getconfig ($config);

$k = Self::allowexist ($data, $_config);

if ($k!== null) {

return $k;

}

foreach ($_config as $k = = $c) {

if (Isset ($data [$k]))

{

if (Isset ($c [' Rexp ']))

{

if (Self::vrexp ($data [$k], $c [' rexp ']) = = = = False) {

return $k;

}

}

if (Isset ($c [' length ']))

{

if (Self::vlength ($data [$k], $c [' length '])

{

return $k;

}

}

if (Isset ($c [' min_length ']))

{

if (!self::vminlength ($data [$k], $c [' min_length ']))

{

return $k;

}

}

if (Isset ($c [' max_length ']))

{

if (!self::vmaxlength ($data [$k], $c [' max_length ']))

{

return $k;

}

}

}

}

return null;

}

private static function Allowexist ($data, $config)

{

foreach ($config as $k = $v)

{

if (!isset ($v [' allow_exist ']) | | $v [' allow_exist '] = = True) {

if (!isset ($data [$k]))

{

return $k;

}

}

}

return null;

}

public static function Vrexp ($data, $rexp) {

$_rexp = Self::getrexp ($REXP);

if (Preg_match ($_rexp, $data) = = False) {

return false;

}

return true;

}

public static function Vlength ($data, $l) {

if (strlen (Trim ($data)) = = $l) {

return false;

}

return true;

}

public static function Vminlength ($data, $l) {

if (strlen (Trim ($data)) < $l) {

return false;

}

return true;

}

public static function Vmaxlength ($data, $l) {

if (strlen (Trim ($data)) > $l) {

return false;

}

return true;

}

public static function Vletternumber ($data) {

if (Preg_match (Self::getrexp (' Letter_number '), $data) = = False) {

return false;

}

return true;

}

public static function Vletternumber_ ($data) {

if (Preg_match (Self::getrexp (' Letter_number_ '), $data) = = False) {

return false;

}

return true;

}

public static function Vnumber ($data) {

if (Preg_match (Self::getrexp (' number '), $data) = = False) {

return false;

}

return true;

}

public static function Vemail ($data) {

if (Preg_match (self::getrexp (' email '), $data) = = False) {

return false;

}

return true;

}

Class Validateparameterconfig

{

public static function GetConfig ($key)

{

' Allow_exist ' =>true or allow_exist does not exist to indicate that the parameter must be passed

$_config = Array (

' Test ' =>array (

' Letter_number ' =>array (' rexp ' = ' letter_number ', ' allow_exist ' =>true),

' Number ' =>array (' rexp ' = ' number ', ' min_length ' =>1' allow_exist ' =>false '),

' Account ' =>array (' rexp ' = ' account ', ' max_length ' =>20),

)

);

if (Isset ($_config[$key])) {

return $_config[$key];

} else {

return $config _not_defind;

}

}

}

Examples of Use:

The parameters sent from the application end are

$arr = Array (' letter_number ' = ' abc123 ', ' account ' = ' acb1234_ ');

Parameter Validation

$_msg = Validation::validate ($arr, ' test ');

if ($_msg!== null) {

Return $_msg. ' is invalid parameter ';

}

http://www.bkjia.com/PHPjc/820421.html www.bkjia.com true http://www.bkjia.com/PHPjc/820421.html techarticle PHP Common validation class and regular regular expression will update the include validateparameterconfig.php in new Time; class Validation {private static function getre XP ($REXP) ...

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