Re: checking to see if value is a integer from a form text field

From: Date: Fri, 16 Feb 2001 01:57:16 +0000
Subject: Re: checking to see if value is a integer from a form text field
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-40055@lists.php.net to get a copy of this message
Data coming from a POST or GET is a string, by definition -- A URL just doens't contain any data type information, and PHP isn't going to sit there and try to guess what the data type of $x should be. You'll need to check its validity as an integer by using something like this: if (strlen($quantity) && ((int) $quantity == (string) $quantity)) && $quantity){ } else{ $quantity = 1; } -- Visit the Zend Store at http://www.zend.com/store/ Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm ----- Original Message ----- From: Joseph H Blythe <joe@binarylogic.com.au> Newsgroups: php.general Sent: Thursday, February 15, 2001 7:32 PM Subject: [PHP] checking to see if value is a integer from a form text field > hey, > > I have a strange problem, when a user inputs the quantity into a text input field in a cart, I want to be able to test to see if the value is a integer if it isn't then default it to 1 > > if ( !is_int($Qty) ) { > $Qty = 1; > } > > Looks ok, should work in theory, but it doesn't if I use this test, the update on the database fails, take it out and it works, so if a user was to type "f" in the quantity field in the cart and hit update because the quantity field is int(2) in the database it will default to 0 which I do not want. I have also tried to set the type before the update like so: > > $Qty = settype($Qty, "integer"); > > This also will make the update fail, how would I make sure a user does not type characters in the quantity field without using javascript or ereg stuff to check the values? I just want to default the quantity to 1 if a invailid quantity is inputted. > > If there is no obvious soulution then I will use a piece of javascript to check the field onSubmit for illegal characters and alert the user I guess. > > Regards, > > Joseph > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#40055) next »