I am very new to regular expression, I did a research and got a little understanding what I need is a password that matches those specifications
what I got to is this
^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)(?!\S)$
which matches 1, 2, 3 specifications but not 4 I tried different stuff on 4, but I failed
can anyone help me ?
You are very close. This seems to solve your problem:
preg_match("/^(?=.*[0-9])(?=.*[a-z])(\S+)$/i", $password)
I made two changes.
i
modifier (match case-insensitive). This allows to remove the A-Z`.(?!\S)
does not really help here, I think. Instead you can simply make your actual match only consist of non-space characters (the \S+
). This will also immediately allow special characters in your password (really anything rexcept for spaces).If you only want to allow a certain set of special characters, replace the \S
by a character class containing letters, digits and all characters you want to allow. By the way, if you want to make sure your password has a certain minimum length, you could change that +
into {8,}
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With