A PHP common form validation class (based on regular)

Source: Internet
Author: User

A regular expression-based PHP common form validation class, gladly with the wind. This form determines the function of the class: To verify that the specified length of the letter/number combination, verify whether the specified length of Chinese characters, ID card number verification, whether it is a specified length of the number, verify the email address, telephone number, verification zip code, URL address, database escape, data format restore, and so on. In the normal development of PHP projects, these are more commonly used Oh, the following code to share to you:

<?PHP/** * page function: Common Forms Validation Class * Author: Gladly with the wind * qq:276624915*/classclass_post{//verifies whether a combination of letters/numbers of a specified lengthfunctionFUN_TEXT1 ($num 1,$num 2,$str){     Return(Preg_match("/^[a-za-z0-9]{".$num 1.",".$num 2."} $/",$str))?true:false;}//verifies whether a number is a specified lengthfunctionFUN_TEXT2 ($num 1,$num 2,$str){     return(Preg_match("/^[0-9]{".$num 1.",".$num 2."} $/i ",$str))?true:false;}//verifies whether the specified length is a Chinese characterfunctionFun_font ($num 1,$num 2,$str){//Preg_match ("/^[\xa0-\xff]{1,4}$/", $string);    return(Preg_match("/^ ([\x81-\xfe][\x40-\xfe]) {".$num 1.",".$num 2."} $/",$str))?true:false;}//Verify the ID numberfunctionFun_status ($str){     return(Preg_match('/(^ ([\d]{15}| [\d] {18}| [\d] {17}x) $)/',$str))?true:false;}//Verify Email addressfunctionFun_email ($str){     return(Preg_match('/^[_\.0-9a-z-][email protected] ([0-9a-z][0-9a-z-]+\.) +[a-z]{2,4}$/',$str))?true:false;}//Verify phone numberfunctionFun_phone ($str){   return(Preg_match("/^ (\ (\d{3}\)) | (\d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7}$/],$str))?true:false;}//Verify ZIP codefunctionFun_zip ($str){   return(Preg_match("/^[1-9]\d{5}$/",$str))?true:false;}//Verify the URL addressfunctionFun_url ($str){   return(Preg_match("/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "\"]) *$/",$str))?true:false;}//Data inbound Escape special character incoming value can be a string or a one-dimensional arrayfunctionData_join (&$data){   if(GET_MAGIC_QUOTES_GPC() ==false)   {    if(Is_array($data))    {     foreach($data  as $k=$v)     {     $data[$k] =addslashes($v); }    }    Else    {    $data=addslashes($data); }   }   Return $data;}//data out-of-Library restore special characters incoming values can be strings or one/two-dimensional arraysfunctionData_revert (&$data){   if(Is_array($data))   {    foreach($data  as $k 1=$v 1)    {     if(Is_array($v 1))     {      foreach($v 1  as $k 2=$v 2)      {      $data[$k 1][$k 2] =stripslashes($v 2); }     }     Else     {     $data[$k 1] =stripslashes($v 1); }    }   }   Else   {   $data=stripslashes($data); }   Return $data;}//data display the restore data format is primarily used for content output the incoming value can be a string or a/two-dimensional array//Data_revert () before executing this method, no need to restore the form contentsfunctionData_show (&$data){   if(Is_array($data))   {    foreach($data  as $k 1=$v 1)    {     if(Is_array($v 1))     {      foreach($v 1  as $k 2=$v 2)      {      $data[$k 1][$k 2]=NL2BR(Htmlspecialchars($data[$k 1][$k 2])); $data[$k 1][$k 2]=Str_replace(" "," ",$data[$k 1][$k 2]); $data[$k 1][$k 2]=Str_replace("\ n", "<br>\n",$data[$k 1][$k 2]); }     }     Else     {     $data[$k 1]=NL2BR(Htmlspecialchars($data[$k 1])); $data[$k 1]=Str_replace(" "," ",$data[$k 1]); $data[$k 1]=Str_replace("\ n", "<br>\n",$data[$k 1]); }    }   }   Else   {   $data=NL2BR(Htmlspecialchars($data)); $data=Str_replace(" "," ",$data); $data=Str_replace("\ n", "<br>\n",$data); }   Return $data;}}?>

Copy the code to save it as: validate_form.class.php, use the include, it's OK.

A PHP common form validation class (based on regular)

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.