我想使用表单验证来要求具有BOTH字母和数字字符的密码。这是我到目前为止所提出的:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric');
问题是“alpha_numeric”要求密码只包含字母或数字,并不需要两者。在这里寻求更强大的密码选项。
答案 0 :(得分:14)
您可以在控制器中设置callback:
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');
答案 1 :(得分:13)
可以在其他项目中轻松重用并本地化验证规则的替代解决方案是扩展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个大写字符和密码中的数字。
这样做的好处是:
答案 2 :(得分:-2)
使用CI 2.2.0,您可以添加参数,例如&#39; alpha_numeric&#39;参数,注意管道(级联)参数:
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();
}
}
在this页面底部附近查找规则参考。