Search code examples
yii2yii2-modelyii2-validation

yii2 password rules pattern


you can at yii2 in the Model Rules enter patterns in passwords ? Tips for a rule that at least one uppercase character and at least one number ? Thanks so much

Rules

['password', 'pattern' => '(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20}'],


Solution

  • Your case probably does not work because for some reason Yii2 does not recognize \d or \p so you have to write this part manually or find a way around.

    I have tested this one:

    /^(?=.*[0-9])(?=.*[A-Z])([a-zA-Z0-9]+)$/
    

    This means it will require at least one upper-case letter and at least one digit (lower-case letters are not necessary).