Eiolon Posted January 6, 2010 Share Posted January 6, 2010 I have a field that asks for a number. Any number is being accepted except for zero. It will accept double zeros but not a single zero. Here is my validation: if (empty($_POST['seats'])) { $errors[] = 'Enter the number of seats available'; } if (!ctype_digit($_POST['seats'])) { $errors[] = 'The number of seats must be a digit'; } if ($_POST['seats']) { $s = escape_data($_POST['seats']); } My escape data function function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysql_real_escape_string (htmlspecialchars(trim(strip_tags($data))), $dbc); } Quote Link to comment https://forums.phpfreaks.com/topic/187473-form-validation-zero-0-not-being-accepted/ Share on other sites More sharing options...
salathe Posted January 6, 2010 Share Posted January 6, 2010 The string "0" is considered as empty (i.e. empty("0") returns TRUE). In the first if statement you could either explicitly cater for "0" or use some other method to check on the string being really empty (e.g. strlen to check the length of the string [in bytes]). Quote Link to comment https://forums.phpfreaks.com/topic/187473-form-validation-zero-0-not-being-accepted/#findComment-989905 Share on other sites More sharing options...
Eiolon Posted January 6, 2010 Author Share Posted January 6, 2010 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/187473-form-validation-zero-0-not-being-accepted/#findComment-989918 Share on other sites More sharing options...
teamatomic Posted January 6, 2010 Share Posted January 6, 2010 Turn the data into a string with: $num = "$_POST['num']"; // note the quotes Then if ($num != NULL) later if you compare your string against an interger PHP will automatically treat the string as an integer. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187473-form-validation-zero-0-not-being-accepted/#findComment-989933 Share on other sites More sharing options...
salathe Posted January 6, 2010 Share Posted January 6, 2010 teamatomic, all $_POST values are strings anway (unless altered in your script) so that would be redundant (plus it's proper to use (string) to cast to a string value). Quote Link to comment https://forums.phpfreaks.com/topic/187473-form-validation-zero-0-not-being-accepted/#findComment-989945 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.