user123456
user123456

Reputation: 105

Validation in cakephp for password is not working

i am using following code in model to validate password in cakephp 2.X

    'password' => array(
    'ruleName' => array(
        'rule' => '(^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$)',
        'message' => "Password should contain: 8 characters, 1 upper case, 1 lower case, 1 number."
    )
),

Can you please tell where I am wrong. on form submit I always get a same message;

Password should contain: 8 characters, 1 upper case, 1 lower case, 1 number.

even if i add valid password like "QWert123"

Upvotes: 1

Views: 501

Answers (1)

Bob
Bob

Reputation: 1497

The regex pattern do not match on this part (?=.*?[#?!@$%^&*-]) for password "QWert123" because you don't have one of the characters in the password. In this case you can make it optional though

'rule' => '(^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]*).{8,}$)',

EDIT: In cakephp2.X, custom validation is used when using regular expression is needed cakephp docs

'rule' => array('custom','(^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]*).{8,}$)'),

Upvotes: 2

Related Questions