Porl123 Posted August 27, 2008 Share Posted August 27, 2008 I've been making this function which checks if the value isn't a decimal, which works. Then I've put is_numeric in there and checked that the value is >= 1 because all I want the user to enter is a whole, numeric, positive number. But for some reason it still allows the user to enter values with a minus on the end e.g. 010000000000000- I'm not really sure at all why it allows them to do that. If someone could help me out with what I'm missing it would be greatly appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 27, 2008 Share Posted August 27, 2008 Show us your code? Quote Link to comment Share on other sites More sharing options...
Porl123 Posted August 27, 2008 Author Share Posted August 27, 2008 Woops, sorry forgot that function whole_int($var) { $var = (int)$var; if($var >= 1 && is_numeric($var)) { $tmp = explode('.', $var); $var = (is_array($tmp)) ? $tmp[0] : $var; } else { $var = 0; } return $var; } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 27, 2008 Share Posted August 27, 2008 I've been making this function which checks if the value isn't a decimal, which works. Then I've put is_numeric in there and checked that the value is >= 1 because all I want the user to enter is a whole, numeric, positive number. But for some reason it still allows the user to enter values with a minus on the end e.g. 010000000000000- I'm not really sure at all why it allows them to do that. If someone could help me out with what I'm missing it would be greatly appreciated. Thanks Numbers which end with a minus are not negatives, negatives start with a minus. Also when you do $var = (int)$var; (int) will remove any non-numeric characters from $var, eg $var = '123abc456'; echo (int) $var; PHP will return $var as 123 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 27, 2008 Share Posted August 27, 2008 I don't know why you have that whole function going on, when you could just do: $number = 64.5; $number = (int) $number; //now it's 64 echo abs($number); //handles negatives Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 27, 2008 Share Posted August 27, 2008 I agree with Wildteen and Darkwater that you can convert the value to an integer and then just validate that it is greater than 0. However, in some instances changing the users entered value like that can be problematic and you should have them correct it themselves. The easiest solution for that (in my opinion) would be a simple regular expression check <?php if (!preg_match("/[^\d]/", $_POST['thevalue'])) { echo "Valid"; } else { echo "Not Valid"; } ?> The value must contain only numbers (0-9) and cannot contain decimals, negative signs, etc. Quote Link to comment Share on other sites More sharing options...
Porl123 Posted August 27, 2008 Author Share Posted August 27, 2008 Ah that's got it working fine now thanks guys for your help 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.