spookztar Posted July 14, 2007 Share Posted July 14, 2007 Hi Guys, new to the forums and nice to be here. Yet another poor soul with a regex prob. I can't get the following code to function properly: if (!preg_match('/[a-z]+/', $formpassword) AND !preg_match('/[A-Z]+/', $formpassword) AND !preg_match('/[0-9]+/', $formpassword)) { echo "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span>"; die($loginform); } elseif (!preg_match('/[a-z]+/', $formusername) AND !preg_match('/[A-Z]+/', $formusername) AND !preg_match('/[0-9]+/', $formusername)) { echo "<span class='warning'>$formusername ERROR: Failed check for required symbols in username. Please adhere to the specifications given.</span>"; die($loginform); } It accepts strings such as "aaaaaaaaaa" without complaining and instead returns an error message from further down the scipt saying "username and/or password incorrect", and it's supposed to demand at least one each of uppercase, lowercase and a digit and die right there if it fails. However, If I process an empty form, it reacts with the "required symbols" message just fine. What am I not getting here? Quote Link to comment Share on other sites More sharing options...
spookztar Posted July 14, 2007 Author Share Posted July 14, 2007 I just found out that if I fill in strings that should be accepted according to the criteria and use {1, } instead of "+", I also get the "failed required symbols" message ??? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 14, 2007 Share Posted July 14, 2007 Merge your regex patterns into one. $error = null; if (!preg_match('/^[a-z0-9]+$/i', $formusername)) { $error .= "<span class='warning'>$formusername ERROR: Failed check for required symbols in username. Please adhere to the specifications given.</span><br />"; } if (!preg_match('/^[a-z0-9]+$/i', $formpassword)) { $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />"; } if(!is_null($error)) { echo $error; die($loginform); } Quote Link to comment Share on other sites More sharing options...
spookztar Posted July 14, 2007 Author Share Posted July 14, 2007 Thanx for replying. Didn't do it. Also, I need it to do, what I want it to do, so changing the nature of the check (i) is not an option. There should be at least one uppercase letter, one lowercase and one digit. Quote Link to comment Share on other sites More sharing options...
spookztar Posted July 14, 2007 Author Share Posted July 14, 2007 Nobody? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 15, 2007 Share Posted July 15, 2007 what about this. $formpassword = "TesT1234"; $valid = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $password)>0)?true:false; if (!$valid) { $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />"; } Quote Link to comment Share on other sites More sharing options...
spookztar Posted July 15, 2007 Author Share Posted July 15, 2007 Thanx for the attempt, but it doesn't nail it. When the name field is processed with "aaaaaaaaaa" and the password field complies it produses no errormessage. when password fails, it produces wrong username. In my own example, replacing the AND's with OR's now gets the job done, but I must say I'm not really too proud about how it's done... Once again, thanx for the effort, I think I'm just gonna marks this one "solved". Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 15, 2007 Share Posted July 15, 2007 Gezzz OK heres the full example <?php $formpassword = "aaaA2aa"; //Works $formpassword = "aaaaaa"; //Fails $valid = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $formpassword)>0)?true:false; if (!$valid) { $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />"; } echo $error; //Added ?> Quote Link to comment Share on other sites More sharing options...
spookztar Posted July 16, 2007 Author Share Posted July 16, 2007 Why "echo $error; //Added" Shouldn't that be a success message? echo "success!; //Added" Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 16, 2007 Share Posted July 16, 2007 I think MadTechie was referring to my code example when he provided you with his code with regards to showing the error message. Perhaps that is why you are getting confused. In my code I changed the way you report errors to the user when their username/password didn't meet your specification. Before you only show'd one error at a time. To be more user friendly I logged all the errors into the error variable and diaplayed all errors in one go. MadTechie's code merged: <?php $formusername = "bbbA2bb"; //Works $formusername = "bbbbbbb"; //Fails $formpassword = "aaaA2aa"; //Works $formpassword = "aaaaaa"; //Fails // set error to a null value $error = null; $username_check = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $formpassword) > 0) ? true : false; $password_check = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $formpassword) > 0) ? true : false; if (!$username_check) { // set username error message $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />"; } if (!$password_check) { // set password error message $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />"; } // check that $error is not set to null // if it is not null we'll display all errors logged if(!is_null($error)) { echo $error; die($loginform); } else { // success username/password adheres to specification // ... rest of code ... } ?> 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.