Search code examples
phpregexvalidationdelimiterpassword-strength

Password strength validating regex error: No ending delimiter or unknown modifier


I am new in regular expression and I was doing some form validation using regular expression. But the problem is most of the regular expression are like:

^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%#a-zA-Z\d]+$

This one I am using for password validation. For other form validation I found lot of such expression here. Now the problem is when I use them in my code as follows

if (preg_match('^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%@#a-zA-Z\d]+$', $password))

I get at least one error. Most of the time it show erro No ending delimiter or unknown modifier etc.


Solution

  • You don't have a delimiter around your expression.

    Try this:

    $pattern = '/^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%#a-zA-Z\d]+$/';
    preg_match ($pattern, $password);