我想使用表单验证来要求同时包含字母和数字字符的密码。到目前为止,这是我想出的:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric');
问题是“alpha_numeric”要求密码只包含字母或数字,它不需要两者。在这里寻求更强的密码选项。
我想使用表单验证来要求同时包含字母和数字字符的密码。到目前为止,这是我想出的:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric');
问题是“alpha_numeric”要求密码只包含字母或数字,它不需要两者。在这里寻求更强的密码选项。
您可以在控制器中设置回调:
public function password_check($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) {
return TRUE;
}
return FALSE;
}
然后,更新您的规则以使用它:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric|callback_password_check');
可以在其他项目中轻松重用并本地化验证规则的替代解决方案是扩展 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]/');
}
}
然后设置规则:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric|password_check[1,1,1]');
该规则可以以许多不同的方式使用。'password_check[1,1,1]' 要求密码包含小写、大写和数字字符。'password_check[5,0,1]' 需要 5 个大写字符和密码中的一个数字。
这样做的好处是:
使用 CI 2.2.0,您可以添加一些参数,例如“alpha_numeric”参数,注意管道(级联)参数:
function check_password(){
$this -> form_validation -> set_rules('password', 'New Password', 'required|max_length[15]|min_length[6]|alpha_numeric');
$this -> form_validation -> set_rules('passconf', 'Re-enter', 'required|matches[password]');
if ($this -> form_validation -> run() == FALSE){
doStuff();
} else {
doOtherStuff();
}
}
在本页底部附近查找规则参考。