Jump to content

[SOLVED] is_numeric not working


Ninjakreborn

Recommended Posts

Yes, it should work however, it's not.

 

Generic tests (ALL numbers) works.

 

Then when I retested, checked on php.net it'sn ot meant to be fullproof.  It's not meant to work as a definitive.  if you have number's, and hten some letter's it let's it got through depending on the combination.

Anybody have something they use that is fullproof?

I would use if(!ereg("[0-9]+",$data)){ //send out an error

 

Or shorten the regex more into "\d+".

 

This will make sure that $data is any number of digits long. If you wanted to limit it you could use "[0-9]{min,max}". However, this won't allow for negatives vs. positives. But you could easily add that by putting a "-" in the very beginning of your character set, i.e. "[-0-9]+" should check all numbers as okay, whether or not they're negative.

// should be true
var_dump(is_numeric('0123.45e052'));
var_dump(is_numeric('0xFF'));

// should be false
var_dump(is_numeric('0123.45e'));
var_dump(is_numeric('01e23.456'));

 

output:

bool(true)

bool(true)

bool(false)

bool(false)

 

as expected, I think is_numeric is fullproof

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.