Re: Validating form field text input to be a specificvariable type
| From: | Red Wingate | Date: | Wed, 07 Apr 2004 15:25:20 +0000 |
| Subject: | Re: Validating form field text input to be a specificvariable type | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182637@lists.php.net to get a copy of this message | ||
An RegExp will work but is not as fast as native functions, but you
could use something like:
$len1 = strlen ( $input );
$len2 = strspn ( $input , "1234567890,." );
if ( $len1 == $len2 ) {
echo 'yuuhooo';
} else {
echo 'd\'oh';
}
[...]
> > From: "William Lovaton" <williama_lovaton@coomeva.com.co>
> >
> > > Instead of doing a lot of casting, you can use is_numeric() to see if a
> > > variable is a number or a numeric string.
> >
> > Yeah, but the OP wanted to be able to tell an integer from a real number
> > from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for
> > example.
>
> well, an integer is a float too, the real problem is when you type 5.5
> in a variable that is expected to be integer. In this case is_numeric()
> won't do the trick.
>
> May be using regexps ([0-9]) will be enough to check integer values but
> I think it is a bit slower. It is always better to use native functions
> instead of regexps where possible.
[...]