Reputation: 59
So I thought I had this right?
if(!preg_match('^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+.{7,}$', $passOne)) {
$msg = "Password does not contain at least 1 number/letter, 8 character minimum requirement.";
}
I test it over at https://regex101.com/ and put ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+.{7,}$
to work and things like RubyGlue12
pass and is a match and other things that aren't.
But no matter what, I cannot make any match in the actual PHP code. Even if the POST is a variable manually.
edit: $_POST['password']
is $passOne
Help?
Upvotes: 2
Views: 8184
Reputation: 599
Here's a regex that tests for what the password should not be, instead of what it should be.
/^(.{0,7}|[^a-z]*|[^\d]*)$/i
Example:
if (preg_match('/^(.{0,7}|[^a-z]*|[^\d]*)$/i', $passOne)) {
echo "Validation failed.\n";
}
Explanation:
There are essentially 3 separate tests within the regex (each separated by a |
, and each are case-insensitive due to the i
option at the end). If it passes any of the tests, then the entire validation fails.
Upvotes: 1
Reputation: 626870
You have .+.{7,}
that does not make much sense since it means match any characters 1 or more times, and then any characters 7 or more times.
1 letter, 1 digit and min 8 characters regex will look like
(?i)^(?=.*[a-z])(?=.*\d).{8,}$
Regex explanation:
(?i)
- Turning case sensitivity off (unless you need to check for 1 uppercase and 1 lowercase letters - then, remove the i
flag and replace (?=.*[a-z])
with (?=.*[a-z])(?=.*[A-Z])
)^
- Start of string(?=.*[a-z])
- Positive look-ahead requirement to have at least 1 letter (?=.*\d)
- Positive look-ahead requirement to have at least 1 digit.{8,}
- At least 8 characters$
- End of string. And PHP code is:
$re = "/^(?=.*[a-z])(?=.*\\d).{8,}$/i";
$passOne = "RubyGlue12";
//$passOne = "eee"; // uncomment to echo the error message
if(!preg_match($re, $passOne)) {
echo "Password does not contain at least 1 number/letter, 8 character minimum requirement.";
}
And yes, with preg_match
function, you must use some regex delimiters. I am using /
here.
Upvotes: 6