michalchojno Posted July 22, 2009 Share Posted July 22, 2009 I have a variable submitted throu html form, input tag. I'd like to check if this variable is integer. I used is_numeric() function, but I get an error message when nothing is input in the window. Will is_int() function work better? There is also ereg() function, but I can't figure out how it really works? What's this: [0-9]{4})-([0-9]{1,2} stuff??? What's the right syntax and logic? Example: ereg("^[1-9][0-9]*$", $_POST['variable']) How can I make it that when nothing is input, I won't get an error and it will be the same as 0 value? Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/ Share on other sites More sharing options...
ignace Posted July 22, 2009 Share Posted July 22, 2009 is_integer() regex is overkill. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880634 Share on other sites More sharing options...
michalchojno Posted July 22, 2009 Author Share Posted July 22, 2009 Not really what I meant. Here is the code I have: if (!ereg("^[1-9][0-9]*$",($_POST[$val]))) { echo "Error."; die(); } When the input field is empty, I get error. How to make it 0 if empty? EDIT: I changed it to the following and it seems to work. if (!ereg("^[1-9][0-9]*$",($_POST[$val])) && $_POST[$val] != 0) { echo "Error."; die(); } Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880644 Share on other sites More sharing options...
michalchojno Posted July 22, 2009 Author Share Posted July 22, 2009 Still could someone please explain how ereg() works? The syntax seems weired. Example: ereg("^[1-9][0-9]*$",($_POST[$val])). Why "^" at the start? Why [1-9[0-9]? Why "*$"? Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880652 Share on other sites More sharing options...
flyhoney Posted July 22, 2009 Share Posted July 22, 2009 http://us2.php.net/manual/en/function.ctype-digit.php might help you in this case. Much easier than regex. <?php $strings = array('1820.20', '10002', 'wsl!12'); foreach ($strings as $testcase) { if (ctype_digit($testcase)) { echo "The string $testcase consists of all digits.\n"; } else { echo "The string $testcase does not consist of all digits.\n"; } } ?> The above example will output: The string 1820.20 does not consist of all digits. The string 10002 consists of all digits. The string wsl!12 does not consist of all digits. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880657 Share on other sites More sharing options...
Daniel0 Posted July 22, 2009 Share Posted July 22, 2009 Still could someone please explain how ereg() works? The syntax seems weired. Example: ereg("^[1-9][0-9]*$",($_POST[$val])). Why "^" at the start? Why [1-9[0-9]? Why "*$"? The regular expressions says: Start of string (^) followed by a character in the range 1-9 ([1-9]) followed by a character in the range 0-9 zero or more times ([0-9]*) followed by end of string ($). Google: "regex" And see: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax By the way, preg is faster than ereg. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880658 Share on other sites More sharing options...
flyhoney Posted July 22, 2009 Share Posted July 22, 2009 ^[1-9][0-9]*$ That is a regular expression. The "^" represents the beginning of the string and "$" represents the end. These are necessary because without them, the above expression would also match strings like: "blah blag 83983 roflcopter". [1-9] matches a number 1-9 obviously. [0-9]* matches any number of numbers 0-9. So it looks like this expression is meant to match any number equal to or greater than 1. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880660 Share on other sites More sharing options...
michalchojno Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks. This is really awesome help with great examples. What actually happens if nothing is input in the input window? The input variable seems empty. And in such case, my code seems to still display an error no matter how I check it for integer. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880712 Share on other sites More sharing options...
9three Posted July 22, 2009 Share Posted July 22, 2009 You need to do if conditions. if (empty($userInt) || !ctype_digit($userInt)) { echo 'A number must be inputted before proceeding'; else { //do whatever you want } Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880729 Share on other sites More sharing options...
flyhoney Posted July 22, 2009 Share Posted July 22, 2009 Just make sure it isn't empty first! $foo = $_POST['foo']; if ($foo and ctype_digit($foo) !== FALSE) { // It's a digit! } Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880730 Share on other sites More sharing options...
michalchojno Posted July 22, 2009 Author Share Posted July 22, 2009 Geeez, frustrating, It doesn't work... If the field is empty (a person didn't put anything in), I'd like to allow it and treat it as 0. Instead I keep getting "Error". Here is what I typed, which doesn't seem to fix it. if (empty($val)) {$val = 0;} if (!ctype_digit($val)) { echo "Error"; die(); } Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880770 Share on other sites More sharing options...
Coreye Posted July 23, 2009 Share Posted July 23, 2009 Try this: <?php if (empty($val)) { $val = "0"; } if (!ctype_digit($val)) { die("Error"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880895 Share on other sites More sharing options...
jcrew Posted July 23, 2009 Share Posted July 23, 2009 You could have done: $int = intval($_POST[whatever]); if(is_numeric($int)){ }// .. etc. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880925 Share on other sites More sharing options...
michalchojno Posted July 23, 2009 Author Share Posted July 23, 2009 No, no... ctype_digit() seems to generate error when the value is 0. I used ereg("^[0-9[0-9]*$", $val) and if empty($val) {$val = 0} and it works. Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880959 Share on other sites More sharing options...
Daniel0 Posted July 23, 2009 Share Posted July 23, 2009 Then try this: if (preg_match('#^(?:0|-?[1-9]\d*)$#', $val)) { } Quote Link to comment https://forums.phpfreaks.com/topic/167020-php-how-to-check-if-the-input-variable-is-integer/#findComment-880999 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.