1

I want to give a pattern for password. The password must be at least 8 characters long and should contain one uppercase letter, one lowercase letter and one number. I am new in yii1. Please help me.

Mohibul Hasan Rana
  • 237
  • 2
  • 3
  • 16

3 Answers3

1

Try this way:

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

You can add any type of Pattern in above code.

Community
  • 1
  • 1
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
  • I give this rules in model but when I input any password and then click save button it inserted in the database. But the message 'Invalid characters in password.' shows in the form. I just want not save if password is not match like pattern. – Mohibul Hasan Rana Sep 22 '15 at 10:57
  • can you add whole form? – Insane Skull Sep 22 '15 at 12:05
0

It seems you can refer some PHP password validation code as follow,

<?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.";
}

For more detail please refer this post

Channa
  • 4,963
  • 14
  • 65
  • 97
0

It can be done by Yii custom validation. Try below this one. i hope the custom validation may useful to your criteria

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