Far Cry Posted June 16, 2011 Share Posted June 16, 2011 function validPassword($password) { $isValid = false; if(strlen($password) >= 6){ if(preg_match('/^[a-zA-Z0-9]{1,}$/',$password)){ $isValid = true; } else{ echo"<center><font color=\"red\">Your password must have at least one number and at least one upper and lower-case letter!</font></center>"; $isValid = false; } } else{ echo"<center><font color=\"red\">Your password must contain at least six (6) characters!</font></center>"; $isValid = false; } return $isValid; } The above code is supposed to validate a password. If (as seen in the code), the string is less than six characters, the else statement executes no problem. However, the else statement for the if statement (preg_match('/^[a-zA-Z0-9]{1,}$/',$password) does not execute, even if the string is valid. I cannot figure this out, thanks in advance! Quote Link to comment Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 if you are going to place a comma inside {1,} there needs to be a max value e.x {1,10} Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted June 16, 2011 Share Posted June 16, 2011 If you want the password to contain at least 1 number, 1 upper-case letter, and 1 lower-case letter it seems like you would need to set the test up differently. Maybe something like: <?php ... if(!preg_match('/[a-z]+/',$password)) { echo"<center><font color=\"red\">Your password must have at least one lower-case letter!</font></center>"; $isValid = false; } elseif(!preg_match('/[A-Z]+/',$password)) { echo"<center><font color=\"red\">Your password must have at least one upper-case letter!</font></center>"; $isValid = false; } elseif(!preg_match('/[0-9]+/',$password)) { echo"<center><font color=\"red\">Your password must have at least one number!</font></center>"; $isValid = false; } else { $isValid = true; } ... ?> Note that the code is untested. Quote Link to comment Share on other sites More sharing options...
Far Cry Posted June 16, 2011 Author Share Posted June 16, 2011 If you want the password to contain at least 1 number, 1 upper-case letter, and 1 lower-case letter it seems like you would need to set the test up differently. Maybe something like: <?php ... if(!preg_match('/[a-z]+/',$password)) { echo"<center><font color=\"red\">Your password must have at least one lower-case letter!</font></center>"; $isValid = false; } elseif(!preg_match('/[A-Z]+/',$password)) { echo"<center><font color=\"red\">Your password must have at least one upper-case letter!</font></center>"; $isValid = false; } elseif(!preg_match('/[0-9]+/',$password)) { echo"<center><font color=\"red\">Your password must have at least one number!</font></center>"; $isValid = false; } else { $isValid = true; } ... ?> Note that the code is untested. Thanks! Works great! :-) Quote Link to comment Share on other sites More sharing options...
xylex Posted June 16, 2011 Share Posted June 16, 2011 The regex method to do something like this would be with assertions. preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$/', $password); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.