FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Needing a Bit Og Help {PHP}!





Ben
Hi!

I need a little PHP script that does the following:

I think you might need to use the eregi() function, but dont know how to code it properly.

I need a script that will validate a number. So you are only allowed numbers - no letters. You are allowed a full stop for a decimal point, buut must have a number each side, and also allowed a hyphen at the start for negative numbers.

Any help is appreciated thanks Razz
Ben
Ranfaroth
Why don't you just use is_numeric function ?
n0obie4life
Code:
preg_replace( "/([^0-9.])/", "", $var )


That's what I did. But it doesn't allow negative values. Maybe try this

Code:
preg_replace( "/([^0-9.-])/", "", $var )
DX-Blog
Ranfaroth wrote:
Why don't you just use is_numeric function ?

is_numeric also allows letters in certican cases, it accepts hexadecimal notations.
Ben
Thanks n0obie4life


Code:

preg_replace( "/([^0-9.-])/", "", $var )


worked fine Razz
Ranfaroth
No : it will accept "1-2" for example.
The correct regex to match is "/^-?[0-9]+(\.[0-9]+)?$/"

DX-Blog, you're right. But personnaly, I'd use floatval...
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.