This question already has answers here:
Match whole string in regex
(2 answers)
Closed 12 days ago.
if (!preg_match("/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,128}/", $password_1)) {
array_push($errors, "Password does not match the requested format");
}
The input for $password_1 is more than 128 characters, but it still results in true. But this expression works with the tag in HTML i.e.
But my doubt is: Why doesn't it work in PHP?
I could try not to use regex, and use manual if statements in php, but it feels like using this regex.
INFORMATION:
I am not good at regex. Please help me with detail.
AND:
I think that regex should match anything that is:
Text more than 6 characters, and less than 128 characters including spaces and stuff
A-Z and a-z and digits.
Special characters
I'm pretty sure there is something wrong with the whole regex. Please help!!
# Answer 1
Within your regex you are missing the start- and end-boundaries (^ and $). Your current regex will just match the first 128 characters that matches your requirements and ignores the rest: https://regex101.com/r/ZLmRHq/1
However if you add the boundaries, you get
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,128}$/
so the match should start at the beginning of the string, and end at the end of the string: https://regex101.com/r/ZLmRHq/3/