This article introduces how to use PHP regular rules to match 6-digit and 16-digit character combinations. This regular rule requires only numbers, letters, and underscores. Friends in need can refer to it.
Requirements: PHP regular matches strings with 6 to 16 characters. Only 6 to 16 characters consisting of numbers, letters, and underscores are allowed. True is returned if applicable, otherwise false is returned. Answer: From 6 to 16 digits, the regular expression can be written like this: {6,16}. The regular expression for any character from 6 to 16 bits is as follows: .{6,16} Only combinations of numbers, letters, and underscores are allowed. The regular rule is: [0-9_a-zA-Z] Put it together, the complete regex is: ^[_0-9a-z]{6,16}$The following is an example of applying this regular verification password. <?php /** * php正则验证密码规则 * 只允许 数字、字母、下划线 * 最短6位、最长16位 * 搜集整理:bbs.it-home.org */ function ispassword($str) { if (preg_match('/^[_0-9a-z]{6,16}$/i',$str)){ return true; }else { return false; } } $password = 'abcde@'; if(ispassword($password)) { echo '符合'; }else { echo '不符合'; } //output 不符合 echo '<br>'; $password = 'abcdeasdas_1324'; if(ispassword($password)) { echo '符合'; }else { echo '不符合'; } //output 符合 ?> Copy after login |