在创建任何用户时,如何在yii1中提供正则表达式模式?

时间:2015-09-22 09:32:44

标签: yii

我想给出一个密码模式。密码长度必须至少为8个字符,并且应包含一个大写字母,一个小写字母和一个数字。我是yii1的新人。请帮帮我。

3 个答案:

答案 0 :(得分:0)

您似乎可以参考以下一些PHP密码验证代码,

<?php

$pwd = $_POST['pwd'];

if( strlen($pwd) < 8 ) {
    $error .= "Password too short! 
";
}

if( strlen($pwd) > 20 ) {
    $error .= "Password too long! 
";
}

if( strlen($pwd) < 8 ) {
    $error .= "Password too short! 
";
}

if( !preg_match("#[0-9]+#", $pwd) ) {
    $error .= "Password must include at least one number! 
";
}


if( !preg_match("#[a-z]+#", $pwd) ) {
    $error .= "Password must include at least one letter! 
";
}


if( !preg_match("#[A-Z]+#", $pwd) ) {
    $error .= "Password must include at least one CAPS! 
";
}



if( !preg_match("#\W+#", $pwd) ) {
    $error .= "Password must include at least one symbol! 
";
}


if($error){
    echo "Password validation failure(your choise is weak): $error";
} else {
    echo "Your password is strong.";
}

有关详细信息,请参阅此post

答案 1 :(得分:0)

尝试这种方式:

public function rules() {
return array(
    array('username, password', 'required'),
    array(
        'password',
        'match', 'pattern' => '/^[\*a-zA-Z0-9]{6,14}$/',
        'message' => 'Invalid characters in password.',
    ),
   array('password', 'length', 'min'=>8),
);
}

您可以在上面的代码中添加任何类型的Pattern

答案 2 :(得分:0)

可以通过Yii自定义验证来完成。 尝试下面这个。我希望自定义验证可能对您的标准有用

public function rules()
{
    return array(
        array('username, password', 'required'),
        array('password', 'length', 'min'=>8, 'max'=>16),
        // custom validation
        array('password', 'checkStrength', 'password'),
    );
}


public function checkStrength($attr)
{
    $policy1 = preg_match('/[A-Z]/', $this->$attr) ? 1 : 0 ;
    $policy2 = preg_match('/[a-z]/', $this->$attr) ? 1 : 0 ;
    $policy3 = preg_match('/[0-9]/', $this->$attr) ? 1 : 0 ;
    $policy4 = preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:\<\>,\.\?]/', $this->$attr) ? 1 : 0 ;

    if(!$policy1)
        $this->addError('password', $this->getAttributeLabel($attr) . ' must contains atleast one upper case character.');

    if(!$policy2)
        $this->addError('password', $this->getAttributeLabel($attr) . ' must contains atleast one lower case character.');

    if(!$policy3)
        $this->addError('password', $this->getAttributeLabel($attr) . ' must contains atleast one number.');

    if(!$policy4)
        $this->addError('password', $this->getAttributeLabel($attr) . ' must contains atleast one special character (/~`!@#$%^&*()_-+={}[]|;:<>,.?)');
}