0

I'm using Wordpress platform and newbie. I made some code for password rule, however, it does not work. I need some your help. Thank you in advance.

elseif (preg_match("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$", $user_signonpassword) !== 1) {
    echo json_encode(array('error' => true, 'message'=> '<div class="wpsm_box warning_type">'.__('Passwords must contain at least eight characters, including at least 1 letter and 1 number.', 'rehub_framework').'</div>'));
    die();
}

Result : show same echo what ever I type in, "Passwords must contain at least eight characters, including at least 1 letter and 1 number."

Eddie
  • 26,593
  • 6
  • 36
  • 58
hiyo
  • 133
  • 3
  • 9
  • Your pattern is right, i think you need to add `!` before match as you want to show error only in case your match fails, i.e. `elseif (!preg_match("^(?=.....` – Code Maniac Apr 19 '19 at 04:01
  • @codeManiac Thank you so much for your help. I used !preg_match as you mentioned and change the code from "^ to '/ and it works well. Does it matter on wordpress or php? I copied that code here maybe regex forum but "^ it does not work. :) – hiyo Apr 19 '19 at 04:30
  • Also remove **!== 1**. You wont need it if you are doing negative check at beginning as suggested by @CodeManiac. – Rahul Apr 19 '19 at 05:24
  • @Rahul Thanks so much! I did :) Is it incorrect way to use !== in php or Wordpress or due to " "? I don't know the reason why my code did not work. – hiyo Apr 19 '19 at 06:34
  • `if (preg_match("/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/", $user_signonpassword)) { return whatever...; }` – Wiktor Stribiżew Apr 19 '19 at 06:44
  • Read [here](https://www.w3schools.com/php/php_operators.asp) for more details. – Rahul Apr 19 '19 at 13:24

0 Answers0