1

I'm trying to validate a password with PHP and I want to use the same Regex with Javascript, In my PHP I'm getting error when I'm trying to use the following regex.(Warning: filter_var(): Compilation failed: range out of order in character class at offset 14)

/^[a-zA-Z0-9_-\]\[\?\/<~#`!@\$%\^&\*\(\)\+=\}\|:\";\',>\{]{4,20}$/

if (!filter_var($password,FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z0-9_-\]\[\?\/<~#`!@\$%\^&\*\(\)\+=\}\|:\";\',>\{]{4,20}$/")))){
    $errors .= "Please enter a valid password.";
}

I want the password to have 0-9,a-zA-Z and these characters ] [ ? / < ~ # ! @ $ % ^ & * ( ) + = } | : " ; ' , > { space plus `

I want to use the regex for front and back ends.

5
  • 7
    Don't limit passwords and use the proper methods to hash and verify passwords with PHP. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack. Commented Sep 22, 2015 at 18:26
  • I'm hashing the password in my user class, $password = password_hash($user_passwd, PASSWORD_DEFAULT); Commented Sep 22, 2015 at 18:29
  • The only problem was the hyphen, right? Place it at the end of the character class, and in JS and PHP you won't have to escape it. Use ^[a-zA-Z0-9_\]\[?\/<~#`!@$%^&*()+=}|:\";\',>{ -]{4,20}$. I see your regex is missing a space, I added it, too. However, the hyphen is missing from your list of allowed characters. Something does not click here. Commented Sep 22, 2015 at 18:47
  • Does it mean my regex works for you? Shall I post an answer then? Commented Sep 22, 2015 at 19:11
  • @stribizhev, yes it does post an answer. Commented Sep 22, 2015 at 19:17

3 Answers 3

2

Take a look at this "modular" regEx

/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/.test(password)

this password should have:

  1. (?=.*\d) = digits
  2. (?=.*[a-z]) = small letters
  3. (?=.*[A-Z]) = cap. letters
  4. .{8,} = at least 8 chars long

Special chars part: taken from one of the answers above thanks @stribizhev

  1. (?=.*[?\/<~#`!@$%^&*()+=}|:\";\',>{ -]) = special chars

So you end up with:

/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[?\/<~#`!@$%^&*()+=}|:\";\',>{ -])/.test(password)

to force minimal lenght .{n,} n = lenght

/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[?\/<~#`!@$%^&*()+=}|:\";\',>{ -]).{8,}/.test(pass)

Sign up to request clarification or add additional context in comments.

3 Comments

I want to include the other mentioned characters.
just add them as additional condition (?=.*[%_])
@TurnTheTables to test your regex you can use the console in chrome dev-tools ;)
0

You can use

^[a-zA-Z0-9_\]\[?\/<~#`!@$%^&*()+=}|:\";\',>{ -]{4,20}$
                                             ^^

Note the "smart" position of the hyphen at the end of the character class, and in JS and PHP you won't have to escape it.

I see your regex is missing a space, I added it, too.

Note that inside a character class, a lot of special characters are treated as literals, and do not have to be escaped. I removed escape symbols from those that do not need escaping.

1 Comment

If you found my answer helpful, please also consider upvoting.
0
/^[a-zA-Z0-9_\]\[\?\/\<\~\#\`\@\$%\^&\*\(\)\+=\}\|:\";\'\,>\{]{4,20}$/

You had an un-escaped hyphen in your first character set, and you needed to escape the comma.

Edit: Escaped a few other metacharacters.

2 Comments

Thanks but you are missing the hyphen.
@Turn The Tables I took it out intentionally because it looked like an error. I used the list of characters you posted underneath your code, which did not include the hyphen. You might want to update your original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.