Jump to content

[SOLVED] is_numeric Function


ballouta

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.