Reputation: 396
preg_match("/^([0-9a-zA-Z])+$/", $newvalue)
I have the above code for preg_match, to check if user input has at least one lower case, one upper case and one numeric value..
but this does'nt seem to work, as it accepts values if they are numeric OR lower case or upper case. and does't make sure that one of each is present
What needs to be fixed?
Upvotes: 0
Views: 6096
Reputation: 2348
Try this.It checks the all criteria of $newvalue containing at least one number, one lower case string and one upper case string
preg_match("(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$)",$newvalue)
Example:
<?php
if(preg_match("(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$)","RoHan123aA"))
{
echo ":) success";
}
else
{
echo ":(";
}
?>
Refer this.It works.
Upvotes: 4
Reputation: 145482
You have just a defined a character class that's applied to the whole string. It does not check that that each individal range is present once. Usually you use three comparisons/preg_match-calls in your PHP code to accomplish this.
But in this case it's also simple to do with some assertions:
preg_match("/^(?=.*[A-Z])(?=.*\d)([0-9a-zA-Z]+)$/", $newvalue)
You can add as many as you want. They are applied at the same time and provide additional match conditions to whatever string the group [0-9a-zA-Z]
already matches.
Upvotes: 0