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

在创建任何用户时,如何在yii1中提供正则表达式模式?,yii,Yii,我想给出一个密码模式。密码长度必须至少为8个字符,并且应包含一个大写字母、一个小写字母和一个数字。我是新来的。请帮帮我。您似乎可以参考以下一些PHP密码验证代码 <?php $pwd = $_POST['pwd']; if( strlen($pwd) < 8 ) { $error .= "Password too short! "; } if( strlen($pwd) > 20 ) { $error .= "Password too long! ";

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

您似乎可以参考以下一些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.";
}
有关更多详细信息,请参阅此

尝试以下方法:

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

您可以在上述代码中添加任何类型的验证。

可以通过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 (/~`!@#$%^&*()_-+={}[]|;:<>,.?)');
}

是否要生成随机的8个字符的密码?你试过什么?没有。我想要用户输入的密码。我在模型中给出了这个规则,但当我输入任何密码,然后单击“保存”按钮时,它会插入数据库。但表单中会显示消息“密码中的字符无效”。我只是不想保存,如果密码不匹配的模式。你可以添加整个表单吗?