cho*_*wwy 6 passwords validation codeigniter codeigniter-2
我想使用表单验证来要求密码具有BOTH字母和数字字符.这是我到目前为止所提出的:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric');
Run Code Online (Sandbox Code Playgroud)
问题是"alpha_numeric"要求密码只包含字母或数字,它不需要两者.在这里寻求更强大的密码选项.
rjz*_*rjz 16
您可以在控制器中设置回调:
public function password_check($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) {
return TRUE;
}
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
然后,更新您的规则以使用它:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric|callback_password_check');
Run Code Online (Sandbox Code Playgroud)
J.M*_*ney 15
可以在其他项目中轻松重用并本地化验证规则的替代解决方案是扩展Form_validation类.
在application/libraries /中创建一个名为MY_Form_validation.php的文件,其中'MY'是您在application/config/config.php中设置的子类前缀.
该类看起来像:
class MY_Form_validation extends CI_Form_validation
{
/**
* Verify that a string contains a specified number of
* uppercase, lowercase, and numbers.
*
* @access public
*
* @param String $str
* @param String $format
*
* @return int
*/
public function password_check($str, $format)
{
$ret = TRUE;
list($uppercase, $lowercase, $number) = explode(',', $format);
$str_uc = $this->count_uppercase($str);
$str_lc = $this->count_lowercase($str);
$str_num = $this->count_numbers($str);
if ($str_uc < $uppercase) // lacking uppercase characters
{
$ret = FALSE;
$this->set_message('password_check', 'Password must contain at least ' . $uppercase . ' uppercase characters.');
}
elseif ($str_lc < $lowercase) // lacking lowercase characters
{
$ret = FALSE;
$this->set_message('password_check', 'Password must contain at least ' . $lowercase . ' lowercase characters.');
}
elseif ($str_num < $number) // lacking numbers
{
$ret = FALSE;
$this->set_message('password_check', 'Password must contain at least ' . $number . ' numbers characters.');
}
return $ret;
}
/**
* count the number of times an expression appears in a string
*
* @access private
*
* @param String $str
* @param String $exp
*
* @return int
*/
private function count_occurrences($str, $exp)
{
$match = array();
preg_match_all($exp, $str, $match);
return count($match[0]);
}
/**
* count the number of lowercase characters in a string
*
* @access private
*
* @param String $str
*
* @return int
*/
private function count_lowercase($str)
{
return $this->count_occurrences($str, '/[a-z]/');
}
/**
* count the number of uppercase characters in a string
*
* @access private
*
* @param String $str
*
* @return int
*/
private function count_uppercase($str)
{
return $this->count_occurrences($str, '/[A-Z]/');
}
/**
* count the number of numbers characters in a string
*
* @access private
*
* @param String $str
*
* @return int
*/
private function count_numbers($str)
{
return $this->count_occurrences($str, '/[0-9]/');
}
}
Run Code Online (Sandbox Code Playgroud)
然后设置规则:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric|password_check[1,1,1]');
Run Code Online (Sandbox Code Playgroud)
该规则可以以许多不同的方式使用.'password_check [1,1,1]'需要密码包含小写,大写和数字字符.'password_check [5,0,1]'需要5个大写字符和密码中的数字.
这样做的好处是:
| 归档时间: |
|
| 查看次数: |
18287 次 |
| 最近记录: |