I'm new to regex.
I need to validate passwords using php with following password policy using Regex:
Passwords:
! @ # $ % *
I have tried the following: /^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]$/
Just get both the password and confirm password fields in the form submit PHP and test for equality: if ($_POST["password"] === $_POST["confirm_password"]) { // success! } else { // failed :( } where password and confirm_password are the IDs of the HTML text inputs for the passwords.
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).
The following matches exactly your requirements: ^(?=.*\d.*\d)[0-9A-Za-z!@#$%*]{8,}$
Online demo <<< You don't need the modifiers, they are just there for testing purposes.
Explanation
^
: match begin of string(?=.*\d.*\d)
: positive lookahead, check if there are 2 digits[0-9A-Za-z!@#$%*]{8,}
: match digits, letters and !@#$%*
8 or more times$
: match end of stringIf 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