Re: Forms : Validating user input is integer

From: Date: Fri, 06 Apr 2001 01:41:37 +0000
Subject: Re: Forms : Validating user input is integer
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-8295@lists.php.net to get a copy of this message
"boclair" <boclair@bigpond.net.au> wrote in message news:002101c0bd8e$30742e60$7ac22dcb@qld.bigpond.net.au... | 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? Brad S Jackson replied suggesting that the php function preg_match() should be used for this type of server side validation. ^^^^^^^^^^^^^^^^^^^^^^ Brad, ----- Original Message ----- From: Brad S. Jackson <bjackson@McLeodUSA.com> To: boclair <boclair@bigpond.net.au> Sent: Thursday, April 05, 2001 11:54 PM Subject: Re: [PHP-DB] Forms : Validating user input is integer > > > This is probably the most efficient way to check. The preg functions are faster > than the ereg functions so I never use ereg. I think server side validation is > required and client side is optional. > > if(preg_match("/^[0-9]+$/", $var)) > echo "Valid"; > else > echo "Invalid"; There you go. Does the same job as Matt William's eregi() I am getting invaluable lessons on this stuff, which I never understood before. Using the php preg_match() function the tested script is _________________________ <? if ((@!$submit) || empty($num) ) { echo "<div align='center'><span class='note'>No entry was made</span></div>"; include "get_id.php"; } if(preg_match("/^[0-9]+$/", $num)) { 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"; } ?> _______________________ Many thanks ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Tim Morris

« previous php.db (#8295) next »