Justtechjobs.com Find a programming school near you






Online Campus Both


php-db | 2001041

Re: [PHP-DB] Forms : Validating user input is integer From: Boclair (boclair <email protected>)
Date: 04/05/01

Ben Udall <ben_udall <email protected>> wrote in message
news:3ACC9A31.887E81ED <email protected>
| 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.
| > 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
| }

Once again, thanks. This validates for an all digit variable. In
this case the tested script reads

<?
if ((@!$submit) || empty($num) )
 {
       echo "<div align='center'><span class='note'>No entry was
made</span></div>";
    include "get_id.php";
 }
if (ereg("^[0-9]+$", $input))
 {
       include "do_form.php";
   }
else
   {
       echo "<div align='center'><span class='note'>The ID should have
been a number.</span></div>";
    include "get_id.php";
 }
?>

Will I ever get on the ball and stay there

Tim Morris

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe <email protected>
For additional commands, e-mail: php-db-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>