Выполнение требований пароля

Я хочу проверить, успешно ли пользователь выполнил следующие требования:

  • Пароль имеет не менее 8 символов
  • Состоит из одной столицы и одной строчной буквы

Как мне это сделать?

Я использую скрипт PHP ниже:

if ( strlen( $password ) < 8 ) { false } else { if ( preg_match( "/[^0,9]/", $password ) ) { // how to check the upper case and lower case } } 

Related of "Выполнение требований пароля"

Вы можете сделать это с помощью регулярного выражения:

 if (!preg_match('/^(?=[az])(?=[AZ])[a-zA-Z]{8,}$/', $password)) { //error } 

Используйте preg_match("/[AZ]/") и preg_match("/[az]/")

 if( strlen($password) < 8 ) { return false; } if(preg_match("/[^0,9]/", $password)) { how to check the upper case and lower case } if($password == strtoupper($password) || $password == strtolower($password)){ //pass fails because its either all upcase, or lowercase } 

Вы можете использовать метод ранжирования пароля:

 $x = "a12ASD!@#$"; $rank = Array(); $rank['length'] = strlen($x); $matches = Array(); preg_match_all("/([az]+)/", $x, $matches); $rank['lowercase'] = strlen(implode('', $matches[0]))/count($matches[0]); $matches = Array(); preg_match_all("/([AZ]+)/", $x, $matches); $rank['uppercase'] = strlen(implode('', $matches[0]))/count($matches[0]); $matches = Array(); preg_match_all("/([0-9]+)/", $x, $matches); $rank['numbers'] = strlen(implode('', $matches[0]))/count($matches[0]); $matches = Array(); preg_match_all("/([^a-zA-Z0-9]+)/", $x, $matches); $rank['symbols'] = strlen(implode('', $matches[0]))/count($matches[0]); echo "<pre>"; var_dump($rank); echo "</pre>"; 

Вы можете использовать обрезку , которая на самом деле намного быстрее, чем регулярное выражение

 if ( trim( $password, 'a..z') != '' && trim( $password, 'A..Z') != '' && strlen($password) >= 8 ) { /* Password validation passes, do stuff. */ } else { /* Password validation fails, show error. */ } 
 if ( strlen($password) >= 8) && preg_match('/[AZ]/', $password) > 0 && preg_match('/[az]/', $password) > 0 ) { /* Password validation passes, do stuff. */ } else { /* Password validation fails, show error. */ } 
 preg_match('/[az]/', $password) && preg_match('/[AA]/', $password)