karldesign Posted April 1, 2008 Share Posted April 1, 2008 Hi all, I am doing a registration for a website and want to check the password meets the following requirements: 1) 6 chars at least 2) A-Z/a-z/0-9 only (no characters) I have worked out how to check for (1) - if(strlen($_POST['formPassword']) < 6) - and I just need to work out how to check for just letters and numbers (2). Any help would be great... Quote Link to comment Share on other sites More sharing options...
karldesign Posted April 1, 2008 Author Share Posted April 1, 2008 Does anyone know if this will be enough..? if(!eregi("[a-z0-9]", $_POST['formPassword'])){ //invalid - not text or numbers } else if(eregi(" ", $_POST['formPassword'])){ //invalid - has space } Quote Link to comment Share on other sites More sharing options...
Orio Posted April 1, 2008 Share Posted April 1, 2008 The best way to check if a string is alphanumeric (Has only letters/numbers) imo is using ctype_alnum(). Example: <?php $str = '12abCD19z4'; if (ctype_alnum($str)) { echo "Letters or digits only"; ?> In the tests I've made in the past, this function was faster than using preg_match() ot eregi(). If you still wanna go for the eregi() way: <?php if(eregi("^[0-9a-z]{6,}$", $_POST['formPassword'])) echo "Letters or digits only, and six chars at least"; ?> This would also check if the string has at least 6 chars Orio. Quote Link to comment Share on other sites More sharing options...
karldesign Posted April 1, 2008 Author Share Posted April 1, 2008 this allows such characters as '@' though... I don't want users to be able to select anythin but letters or numbers... the ctype_alnum() function works fine though. thanks 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.