gamefreak13 Posted May 24, 2008 Share Posted May 24, 2008 I need to make sure the password is between x and y (lets just say 4 and 12) characters long. However, I believe the user should be able to input anything they want for their password, including crazy stuff like "$@1dogsrule1@$". All I care to check is that the that it is between 4 and 12 characters long. I know this would work for letters and numbers, but I don't want to dictate what characters are allowed/disallowed. I tried removing the [a-z0-9] part but it didnt work. Same with removing it plus the (). ^([a-z0-9]{4,12})$ I know I could use strlen, but that would require two IF statements.. one for "if smaller than 4" and one for "if larger than 12"... so a regex seems like less code which thus seems like a better choice. Quote Link to comment Share on other sites More sharing options...
Orio Posted May 24, 2008 Share Posted May 24, 2008 I know I could use strlen, but that would require two IF statements.. one for "if smaller than 4" and one for "if larger than 12"... so a regex seems like less code which thus seems like a better choice. That's a mistake. Using strlen() is much more efficient than using preg_match(). <?php $len = strlen($pass); if($len < 4 || $len > 12) echo "Bad pass!"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks.. I set up some strlen's like you said and it works great. In an effort to not make too many threads.. this seems to work but I just wanted to double check to make sure I have it right because I wrote this regex myself. I want "numbers only OR empty cause this field is not required". if(!preg_match('/^[0-9]+$|^$/', $friendid)) { echo "<li>Your FriendID can only contain numbers</li>\n"; } Quote Link to comment Share on other sites More sharing options...
sasa Posted May 25, 2008 Share Posted May 25, 2008 try if(!preg_match('/^[0-9]*$/', $friendid)) { echo "<li>Your FriendID can only contain numbers</li>\n"; } 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.