You are here: Re: [PHP] is_numeric « PHP « IT news, forums, messages
Re: [PHP] is_numeric

Posted by Richard Lynch on 10/21/89 11:15

On Wed, May 4, 2005 3:51 am, pete M said:
> not a php expert but have filed this bug report re validating
> is_numeric('3e0');
> http://bugs.php.net/bug.php?id=32943
>
> Now tried
>
> function isnumeric($n) {
> if (ereg("^[0-9]{1,50}.?[0-9]{0,50}$", $n)) {
> return true;
> } else {
> return false;
> }
> }
>
> and that doent seem to work either..
>
> any ideas.. need to validate anything without 0-9 and a dot within

.. is a special character in Regex, so your .? is going to match: "any
single character, or nothing"

You need \. for the Regular Expression, only you need \\ in PHP to get \

Plus $ would be interpreted as the start of variable inside of "" in PHP,
so to be really clear, I'd escape that as well:

"^[0-9]{1,50}\\.?[0-9]{0,50}\$"

The 50-character limit seems rather arbitrary and silly, actually...

Perhaps just a + and * instead of the {1,50} and {0,50} as well.

You've also completely missed all the negative numbers, by the way.

Maybe you want to read through the User Contributed notes at
http://php.net/ereg and http://php.net/pcre and find a solution that more
closely matches the real world.

For that matter, Euler notation is quite common, and maybe it's really
best if you just use the built-in function, and accept that the 'e'
notation is valid.

YMMV

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация