justlukeyou Posted March 30, 2011 Share Posted March 30, 2011 I have a number of Forms which I want to control the input, for example prevent people from using numbers or ensure that people use specific characters. For example "Your password must contain a capital letter and at least one number". Does anyone know what code I should use to do this. Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/ Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 regular expressions can do this so preg_match() or the ctype function ctype_alnum() Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194547 Share on other sites More sharing options...
justlukeyou Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks, I am using pregmatch, can you advise how I block characters such as $1) etc and limit the number of characters. For example: "Your Username must not exceed 15 characters" if(preg_match("/[a-zA-Z0-9]/", $membername) == 0 && !$error) { Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194571 Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 you might want to and the start ^ and end $ of the string. if (preg_match("/^[a-zA-Z0-9]+$/", $membername) == 0 && !$error) { that way it will only pass this test if the whole string consists of nothing but those characters. for the length thing, strlen() can tell you the length of the string. Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194577 Share on other sites More sharing options...
justlukeyou Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks, How do I execute strlen(), do I use it in a preg match to issue an error message if(preg_match($membername= 'abcdef'; echo strlen($str); //15 == 0 && !$error) { $error = "The username must contain no more than 15 characters."; Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194586 Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 erm, no you do it as a separate condition if (strlen($str) > 15){ echo 'username too long AHHHHH'; } Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194590 Share on other sites More sharing options...
justlukeyou Posted March 30, 2011 Author Share Posted March 30, 2011 Brilliant thanks, I have this but I want spit out as error which I am currently doing with lots of other errors. I have $error set in a table which controls the font so it would be handy if I could continue to use it.. if (strlen($membername) > 15) && !$error) { $error = "The username must not exceed 15 characters."; } I have this working: if((!isset($password2) || empty($password2)) && !$error) { $error = "You need to enter your password twice."; } Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194595 Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 that looks fine, although there is no need to check !$error all the time... you can just do if (strlen($membername) > 15){ $error = 'The username must not exceed 15 characters.'; }else if (empty($password2)){ $error = 'You need to enter your password twice.'; } and so on for all of the conditions. Link to comment https://forums.phpfreaks.com/topic/232216-control-form-input/#findComment-1194597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.