Helper Helper - 11 months ago 115
PHP Question

Regex Validation in Laravel 5.2

Below is my rule for password:

return [
'Password' => 'required|min:8|max:100|regex:[a-z{1}[A-Z]{1}[0-9]{1}]',
'Password_confirmation' => 'required|min:8|max:100|regex:[a-z{1}[A-Z]{1}[0-9]{1}]',

I am trying to add the rule such that it must have

  1. atleast one small char

  2. atleast one small char

  3. atleast one number

  4. atleast one special char

  5. min 8 chars

I tried this and it works
, on a regex tester software . but not sure why it does not work in Laravel

Am I missing something ?

Answer Source


return [
    'password' => [

Firstly, you do not need to check the confirmation separately. Just use the confirmed rule.

The expression you were using was invalid, and had nothing to do with what you wanted. I do suggest you do some research on regular expressions.

Due to the fact that the expression shown above uses pipes (|), you can specify the rules using an array.

Lastly, it is not recommended to allow long passwords as they may be forgotten easily. Best to set a lower limit - I have set it to 20 here.

Edit: You could also use this expression, which appears to have been tested a little more thoroughly.