ballouta Posted May 25, 2009 Share Posted May 25, 2009 Hi I am trying to validate a phone number just to make sure that the users are writing numbers not letters. I am using this line of code: <?php if (!is_numeric ($phone)) echo "Error"; else echo "OK"; ?> I noticed that if i insert +123456, the code continues and insert in the database this number: 2147483647 this field is called phone in my DB and of length 25 what's the problem and where? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/ Share on other sites More sharing options...
GingerRobot Posted May 25, 2009 Share Posted May 25, 2009 The is_numeric function will allow signed numbers. It'll also allow decimal and exponential parts. If you want to ensure the string only contains decimal digits, use the ctype_digit function. As for why it's been inserted as 2^32-1; i'm not overly sure. Evidently there's some sort of overflow going on somewhere. What type is the field? Do you do anything else to $phone before it's inserted? Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/#findComment-841679 Share on other sites More sharing options...
ballouta Posted May 25, 2009 Author Share Posted May 25, 2009 thank you, i just read the ctype_digit function manual. The phone number in my database is of type int(25) the main problem is clear now. Is it possible to allow the user to write only the sign (+) before the phone number, i mean is there a built in fucntion that validates the phone number and allows the + sign before it? or this requires a reg? If yes what type should the phone field be in the DB? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/#findComment-841688 Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 $num = '+12345'; if (preg_match('~^\+?[0-9]+$~',$num)) // valid else // not valid Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/#findComment-841689 Share on other sites More sharing options...
ToonMariner Posted May 25, 2009 Share Posted May 25, 2009 perhaps you might want to cater for a varchar instead? Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/#findComment-841690 Share on other sites More sharing options...
jxrd Posted May 25, 2009 Share Posted May 25, 2009 Yeah, if it's an int and the first digit is a 0 it removes it. Or so I think. Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/#findComment-841705 Share on other sites More sharing options...
ballouta Posted May 25, 2009 Author Share Posted May 25, 2009 Thank you all the reg. exp works prefect Quote Link to comment https://forums.phpfreaks.com/topic/159565-solved-is_numeric-function/#findComment-841707 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.