mike177 Posted January 17, 2008 Share Posted January 17, 2008 Hi, I am having some trouble with my alphanumeric error checking on passwords with my login script. I am using the following to check the password, its the last "else if" statment, I thought it would be helpful to include the whole proccess: /*Password error checking*/ $field = "pass"; if(!$subpass){ $form->setError($field, "* Password Not Entered"); } else{ $subpass = stripslashes($subpass); if(strlen($subpass) < { $form->setError($field, "* Password Must Contain Between 8-15 Characters"); } else if(strlen($subpass) > 15){ $form->setError($field, "* Password Must Containe Between 8-15 Characters"); } else if(!ereg("([0-9A-Za-z])", ($subpass = trim($subpass)))){ $form->setError($field, "* Password Must Be Alphanumeric"); } } Note: I am also trimming the password, I've tried removing the trim but that had no effect. I'm not receiving any error messages, it just enteres the password in the database alphanumeric or not. Any suggestions. Thanks in advance for any help. Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/ Share on other sites More sharing options...
phpSensei Posted January 17, 2008 Share Posted January 17, 2008 Where is the $subpass var? Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/#findComment-442115 Share on other sites More sharing options...
mike177 Posted January 17, 2008 Author Share Posted January 17, 2008 Where is the $subpass var? Its just the password from the user input form and its pasted from proccess but it has no other major operations so I didn't include it. Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/#findComment-442153 Share on other sites More sharing options...
inet411 Posted January 17, 2008 Share Posted January 17, 2008 untested but try replacing: else if(!ereg("([0-9A-Za-z])", ($subpass = trim($subpass)))){ with else if(!ereg("([^0-9A-Za-z])", ($subpass = trim($subpass)))){ Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/#findComment-442160 Share on other sites More sharing options...
revraz Posted January 17, 2008 Share Posted January 17, 2008 Use ctype instead of regex. Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/#findComment-442162 Share on other sites More sharing options...
roopurt18 Posted January 17, 2008 Share Posted January 17, 2008 You could use ctype as revraz suggested, but the problem with your first attempt was the wrong regexp: ([0-9A-Za-z]) 1) Minor note, but the parens aren't needed. 2) The problem, that regexp matches a single alphanumeric character. Add a + to it if you want it to match one or more. [0-9A-Za-z]+ (edit) While I'm at it, might as well write the regexp to eliminate all of your if statements: /^[0-9A-Za-z]{8,15}$/ That matches all alphanumeric strings 8 to 15 characters in length with no beginning or trailing white space. if(!preg_match('/^[0-9A-Za-z]{8,15}$/', $pass)){ // BAD PASSWORD } Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/#findComment-442239 Share on other sites More sharing options...
mike177 Posted January 17, 2008 Author Share Posted January 17, 2008 You could use ctype as revraz suggested, but the problem with your first attempt was the wrong regexp: ([0-9A-Za-z]) 1) Minor note, but the parens aren't needed. 2) The problem, that regexp matches a single alphanumeric character. Add a + to it if you want it to match one or more. [0-9A-Za-z]+ (edit) While I'm at it, might as well write the regexp to eliminate all of your if statements: /^[0-9A-Za-z]{8,15}$/ That matches all alphanumeric strings 8 to 15 characters in length with no beginning or trailing white space. if(!preg_match('/^[0-9A-Za-z]{8,15}$/', $pass)){ // BAD PASSWORD } Cheers mate, Works a treat. Link to comment https://forums.phpfreaks.com/topic/86529-solved-alphnumeric-checking/#findComment-442277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.