PHP regular expression matching the specified length of characters must contain numbers, letters, and underscores

Source: Internet
Author: User
Tags chr php regular expression

Any characters 6 to 16 are. {6, 16}

Only numbers, letters, and underscores are composed of [0-9_a-zA-Z]

Add the total to pepper ^ [_ 0-9a-z] {6, 16} $

The code is as follows: Copy code

<? Php
Function ispassword ($ str ){
If (preg_match ('/^ [_ 0-9a-z] {6, 16} $/I', $ str )){
Return true;
} Else {
Return false;
 }
}
$ Password = 'abcde @';
If (ispassword ($ password )){
Echo 'conformances ';
} Else {
Echo 'nonconformity ';
}
// Output does not match

 

Echo '<br> ';
$ Password = 'abcdeasdas _ 000000 ';
If (ispassword ($ password )){
Echo 'conformances ';
} Else {
Echo 'nonconformity ';
}
// Output
?>

Example 2

The code is as follows: Copy code


$ Action = trim ($ _ get ['action']);
If ($ action = "sub ")
{
$ Str = $ _ post ['dir'];
// If (! Preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "A-Za-z0-9 _] + $/", $ str) // gb2312 regular expression of Chinese characters, letters, numbers, underscores (_)
If (! Preg_match ("/^ [x {4e00}-x {9fa5} A-Za-z0-9 _] + $/u", $ str) // regular expression of UTF-8 Chinese characters, letters, numbers, underscores (_)
    {  
Echo "<font color = red> the [". $ str. "] you entered contains illegal characters </font> ";
    }
Else
    {
Echo "<font color = green> the [". $ str. "] you entered is completely legal. Pass! </Font> ";
    }
}
?>
<Form method = "post" action = "">
Input characters (numbers, letters, Chinese characters, and underlines ):
<Input type = "text" name = "dir" value = "">
<Input type = "submit" value = "submit">
</Form>

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.