Porl123 Posted October 11, 2009 Share Posted October 11, 2009 I made a function which makes sure all numbers entered only contain the characters 0-9 but for some reason if you enter random numbers it returns false function validno($var) { if(!is_numeric($var)) { $ret = 0; } elseif($var < 0) { $ret = 0; } elseif(!preg_match('/^[0-9]+$/', $var)) { $ret = 0; } else { $ret = 1; } return $ret; } It returns false if you enter 12000000000, 14000000000, 17000000000 that I'm aware of. Probably more but I'm not sure. Any see the problem? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/ Share on other sites More sharing options...
Porl123 Posted October 11, 2009 Author Share Posted October 11, 2009 Would ctype_digit() do the job that I want this to do btw? Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934889 Share on other sites More sharing options...
Daniel0 Posted October 11, 2009 Share Posted October 11, 2009 If you want positive integers without leading zeroes you can do this: preg_match('#^[1-9][0-9]*$#', $str); If you want to include 0 as well you can do: preg_match('#^(?:[1-9][0-9]*|0)$#', $str); Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934891 Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 how is $var being assigned in the first place? My guess is that you end up with a large number that ends up getting stored as 6.0221415e23 or something. This is considered numeric, but will fail your preg_match. Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934896 Share on other sites More sharing options...
Porl123 Posted October 11, 2009 Author Share Posted October 11, 2009 I just realised it wasn't in that function it was in the script. I'm not sure why but 12000000000 was converted to 1.2E+10 and when I used intval to get the value I saw intval has a limit of 2147483647 If anyone knows any other functions I'd really appreciate you helping me Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934905 Share on other sites More sharing options...
Porl123 Posted October 11, 2009 Author Share Posted October 11, 2009 No ideas? can't think of anything else Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934938 Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 how is $var being assigned in the first place? My guess is that you end up with a large number that ends up getting stored as 6.0221415e23 or something. This is considered numeric, but will fail your preg_match. Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934941 Share on other sites More sharing options...
Porl123 Posted October 11, 2009 Author Share Posted October 11, 2009 Yeah it's shown as 1.2E+10. so would php see what this represents or would I need to convert it? Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934946 Share on other sites More sharing options...
Daniel0 Posted October 11, 2009 Share Posted October 11, 2009 You can use sprint(). It's being converted to a float because it overflows the range of integers. Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934950 Share on other sites More sharing options...
Porl123 Posted October 11, 2009 Author Share Posted October 11, 2009 Ah yeah that sorted it thanks for all the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934971 Share on other sites More sharing options...
Daniel0 Posted October 11, 2009 Share Posted October 11, 2009 You can use sprint(). That was supposed to have been sprintf(), but I see you solved it anyways Quote Link to comment https://forums.phpfreaks.com/topic/177310-solved-problem-with-number-verification/#findComment-934972 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.