PHP validation can only enter regular expressions for Chinese characters, English, and numbers

Source: Internet
Author: User


A regular expression is collected. You can verify that you can only enter numbers, Chinese characters, and English. Separate validation can also be integrated together to verify. But I was torn apart. better. can be verified separately. After my test can be used Oh! The following is the PHP validation can only input Chinese characters, English, digital code

<?php

if (Preg_match ('/^[0-9]+$/', $str)) {
Echo ' value can enter number ';
}

if (Preg_match ('/^[a-za-z]+$/', $str)) {
Echo ' can only be entered in English ';
}

Verify Chinese

Utf-8 under

Preg_match ("/^[a-z0-9_x80-xff]+[^_]$/g", $a);

if (Preg_match ("/^[\x7f-\xff]+$/", $str)) {
Echo ' can only input Chinese ';
}

GBK under:

Preg_match ("/^[a-z0-9_". Chr (0XA1). " -". Chr (0xFF)."] +[^_]$/", $a)

?>

Example

?
function Checkstr ($STR) {
$output = ';
$a =ereg (' [Chr 0xa1 '). -'. Chr (0xff). '] ', $str);
$b =ereg (' [0-9] ', $str);
$c =ereg (' [a-za-z] ', $str);
if ($a && $b && $c) {$output = ' mixed string ' in Chinese character digital English;}
ElseIf ($a && $b &&! $c) {$output = ' mixed string of kanji numbers ';}
ElseIf ($a &&! $b && $c) {$output = ' mixed string ' in English characters ';}
ElseIf (! $a && $b && $c) {$output = ' mixed string in digital English ';}
ElseIf ($a && $b &&! $c) {$output = ' pure Chinese ';}
ElseIf (! $a && $b &&! $c) {$output = ' pure number ';}
ElseIf (! $a && $b && $c) {$output = ' plain English ';}
return $output;
}

Example

PHP regular match numbers, Chinese and English, middle horizontal, underline, utf-8

Function check_string ($nickname) {
    $nickname = Trim ($nickname);
   
& nbsp;   $pattern = "# ([0-9a-za-z-_x{4e00}-x{9fa5}]+) #iu";
    if (empty ($nickname) | |!preg_match ($pattern, $nickname)) {
         return FALSE;
   }
   
    if (strlen $nickname) < 4 | | strlen ($NICKNAME) >) {
&NBSP;&NB sp;      return FALSE;
   }
   
    return TRUE;
}

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.