如何在 yii 中制定字母数字规则
How to make alphanumeric rules in yii
我需要字母数字规则,我需要输入密码,密码必须是字母数字。任何人都可以对此有想法吗?
将此添加到您的模型中 class,
public function rules()
{
return array(
array('password', 'compare', 'on'=>"confirmpassword", 'compareAttribute'=>'password'),
array('password','passwordalphanumeric','on'=>'changepassword'),
);
}
// Check password with alphanumeric validation
public function passwordalphanumeric($attribute_name,$params){
if(!empty($this->password)){
if (preg_match('~^[a-z0-9]*[0-9][a-z0-9]*$~i',$this->password)) {
// $subject is alphanumeric and contains at least 1 number
} else { // failed
$this->addError($attribute_name,'Please enter password with digits');
}
}
(或)
您也可以使用 this 扩展程序。
我需要字母数字规则,我需要输入密码,密码必须是字母数字。任何人都可以对此有想法吗?
将此添加到您的模型中 class,
public function rules()
{
return array(
array('password', 'compare', 'on'=>"confirmpassword", 'compareAttribute'=>'password'),
array('password','passwordalphanumeric','on'=>'changepassword'),
);
}
// Check password with alphanumeric validation
public function passwordalphanumeric($attribute_name,$params){
if(!empty($this->password)){
if (preg_match('~^[a-z0-9]*[0-9][a-z0-9]*$~i',$this->password)) {
// $subject is alphanumeric and contains at least 1 number
} else { // failed
$this->addError($attribute_name,'Please enter password with digits');
}
}
(或)
您也可以使用 this 扩展程序。