-2

I have to make a regex for a password with the following conditions: - minimum 4 characters - at least 1 non-word character.

I tried this way:

$regex_password  = '@(\W)+@';
$regex_password1 = '@(?i)([a-z\d])+@';

if ((!preg_match($regex_password, trim($_POST['pass']))) && (!preg_match($regex_password1, trim($_POST['pass']))) && strlen(trim($_POST['pass'])) <5){
//error
}

And then tried to create a new account with the password: "pas" and it's working, so there's something wrong with my regex. Can someone help?

Elena Stoean
  • 85
  • 1
  • 10
  • Possible duplicate of [RegEx to make sure that the string contains at least one lower case char, upper case char, digit and symbol](https://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper) – RAUSHAN KUMAR Aug 25 '17 at 12:23
  • If you try with "pas" its falling through the 4 char minimum condition – Marvin Fischer Aug 25 '17 at 12:24
  • This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. – Script47 Aug 25 '17 at 12:29

1 Answers1

2

You just have to change your && to || i.e. if any of condition false then it will echo an error

if ((!preg_match($regex_password, trim($_POST['pass']))) || (!preg_match($regex_password1, trim($_POST['pass']))) || strlen(trim($_POST['pass'])) <5){
echo "error";
}
else
{
   echo "ok";
}
B. Desai
  • 16,414
  • 5
  • 26
  • 47