Porl123 Posted January 27, 2009 Share Posted January 27, 2009 I'm trying to make my own function that checks a few features of a number for validation of whole numbers. I just want it to give a boolean response of valid or not valid. The checks would be that it only contains 0-9, no other characters whatsoever, that it was more than 0 and preferably without any limits. If anyone can help me here I'd greatly appreciate it, thanks! Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/ Share on other sites More sharing options...
rhodesa Posted January 27, 2009 Share Posted January 27, 2009 function is_pos_int ( $number ) { return (is_numeric($number) && $number > 0); } Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/#findComment-747612 Share on other sites More sharing options...
Porl123 Posted January 27, 2009 Author Share Posted January 27, 2009 I've tried using is_numeric before but it still seems to allow decimels Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/#findComment-747615 Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 Hummm is_numeric('0.123') // = true Try this $data = "111"; if (preg_match('/^[0-9]$/', $data)) { //its a number only } <?php function isnumeric($data) { return preg_match('/^[0-9]$/', $data); } ?> Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/#findComment-747617 Share on other sites More sharing options...
rhodesa Posted January 27, 2009 Share Posted January 27, 2009 ah...good call function is_pos_int ( $number ) { return preg_match('/^[1-9]\d*$/',$number); } edit: this one doesn't allow 0 Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/#findComment-747618 Share on other sites More sharing options...
Porl123 Posted January 27, 2009 Author Share Posted January 27, 2009 Ah, thanks guys for your help. :] Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/#findComment-747621 Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 Ahh forgot about the No zero! Link to comment https://forums.phpfreaks.com/topic/142638-solved-integer-functions/#findComment-747623 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.