如何在yii中创建字母数字规则


How to make alphanumeric rules in yii

我需要字母数字规则,我需要输入密码,密码必须是字母数字。有人对此有想法吗?

将其添加到您的模型类中

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');
     } 
}

(或)

你也可以使用这个分机。