bachx Posted July 15, 2007 Share Posted July 15, 2007 I've been trying to check for the number types entered by the user in case they're floats or not with no luck. I don't want the user for example to be able to enter 0.6 or .9, only real numbers should be allowed. I've tried is_float(), is_int() and is_real() with no luck, they either allow all numbers or pervent all. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/60068-preventing-floatsfractions-numbers/ Share on other sites More sharing options...
sasa Posted July 15, 2007 Share Posted July 15, 2007 is_numeric() Quote Link to comment https://forums.phpfreaks.com/topic/60068-preventing-floatsfractions-numbers/#findComment-298772 Share on other sites More sharing options...
keeB Posted July 15, 2007 Share Posted July 15, 2007 You can try to convert what the user is typing in to an int using 1 of 2 methods: Type casting <?php $x = 0.5; print (int) $x; ?> Function <?php $x = 0.5; print intval ($x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/60068-preventing-floatsfractions-numbers/#findComment-298787 Share on other sites More sharing options...
bachx Posted July 15, 2007 Author Share Posted July 15, 2007 I don't want to convert the user input, I want to simply prevent anything but integers. Is that possible? is_numeric(), nor the other functions I listed doesn't seem to do that. Quote Link to comment https://forums.phpfreaks.com/topic/60068-preventing-floatsfractions-numbers/#findComment-299043 Share on other sites More sharing options...
cooldude832 Posted July 15, 2007 Share Posted July 15, 2007 yeah, but fyi real numbers are 1,2,4.3,6.23423.2/4,6/23 non real numbers are 5sqrt(-1) 654sqrt(-1) or 654i f Any way try <?php $num = $_POST['num']; $round = round($num); if ($round == $num && is_finite($round)){ echo "Its a non decimal number containing a finite value have fun."; else{ echo "Its in valid size"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60068-preventing-floatsfractions-numbers/#findComment-299050 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.