In article <[EMAIL PROTECTED]>,
[EMAIL PROTECTED] (Richard) wrote:
> Hi, I want to validate that a variable only contains numbers. I came up with
> this code
>
> $variable = "154545"; -> must give me true.
> $variable = "$%54545"; -> must give me false
> $variable = "$%54545"; -> must give me false
> $variable = "4545;#"; -> must give me false
> $variable = "004545"; -> must give me false
Assuming that you actually wanted true on that last one, any of the
following should do:
if(preg_match('/^[0-9]+$/',$variable))
if(preg_match('/^\d+$/',$variable))
if(ereg('^[0-9]+$',$variable))
if(ereg('^[[:digit:]]+$',$variable))
If you don't consider zero a valid "number", you can use either of these
instead:
if(preg_match('/^[1-9]+$/',$variable))
if(ereg('^[1-9]+$',$variable))
--
CC
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]