RE: [PHP] Checking for Valid Charactors.
| From: | John W. Holmes | Date: | Sun, 08 Jun 2003 14:44:16 +0000 |
| Subject: | RE: [PHP] Checking for Valid Charactors. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-150670@lists.php.net to get a copy of this message | ||
> From: Jason Wong [mailto:php-general@gremlins.biz]
> > On Sunday 08 June 2003 16:22, Philip J. Newman wrote:
> > I would liek to check for 0-9 and . charactors i'm using ...
> >
> > $email = "60.00";
> >
> > if eregi("^[0-9.])?$",$email) {
> >
> > echo"valid";
> >
> > } else {
> >
> > echo"not valid";
> >
> > }
> >
> > Its not working well. umm ... help ...
>
> eregi("^([0-9.])*$", $string)
>
> will match:
> an empty string
> a string containing only 0-9 and periods
>
> It is better for you to learn the preg_*() functions instead of the
> ereg*()
> ones.
FYI, that code will also allow a string such as "99.4.3....223...04..."
If that's not desirable, you could look into using the is_float() or
is_numeric() functions, or a regex such as:
if(preg_match('/^[0-9]*\.?[0-9]+$/',$string))
{ echo "good"; }
else
{ echo "bad"; }
That will allow text/numbers with or without a period.
---John W. Holmes...
Amazon Wishlist: http://www.amazon.com/o/registry/3BEXC84AB3A5E
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/