Re: Forms : Validating user input is integer
| From: | Ben Udall | Date: | Thu, 05 Apr 2001 16:15:45 +0000 |
| Subject: | Re: Forms : Validating user input is integer | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-8277@lists.php.net to get a copy of this message | ||
boclair wrote:
> I have a problem with a user input in a form required to be an
> integer, creating a variable for a mysql query.
>
> I have instances where integer, 0, is being typed as letter,o.
>
> The last discussion of validating the input was
> http://marc.theaimsgroup.com/?l=php-db&m=97207172003983&w=2ö®
> þeÀü´‡Ä†9b
>
> There it was suggested that the only way to validate the input was
> using javascript. I have a case where clients have disabled
> javascript.
>
> I tried fiddling with is_int($input) but of cause it does not serve
> this purpose.
>
> Does anybody know if such validation can be done server side or
> definitely must be done client side?
A basic client-server rule is to never trust the client. You should be
doing all validation on the server side. Client side validation is a
nice feature, but should be in addition to the sever side checking and
never required.
Here's the quickest way I know to validate an integer in php:
if (ereg("^[0-9]+$", $input))
{
// $input is a valid non-negative integer
}
or, if negative numbers are valid, use could use this one:
if (ereg("^-?[0-9]+$", $input))
{
// $input is a valid integer
}
Hope this helps.
-Ben Udall