Page 1 of 1

How to validate if a variable is an integer or not

Posted: Tue Nov 05, 2002 9:55 pm
by rax369
Hi guys :!: I'd like to ask for ur help this time, with a simple validation, the code below for my logic should work, but it doesnt.

The question is very simple, I have 10 text boxes in a form where the user can type the quantity of a product to request. But since I dont wanna allow to enter characters, only numbers >= 1, I'm checking the condition with this code (the quantity of the product requested coming from the form is stored in the array $products_cant):

Code: Select all

for ($i=0; $i<=9; $i++)
&#123;
   if (is_int($products_cant&#1111;$i]))
   &#123;
       if ($products_cant&#1111;$i] >= 1)
       &#123; $GO = 1; &#125;
       else
       &#123; $GO = 0; &#125;
   &#125;
   else
   &#123;$GO = 0;&#125;
&#125;
The code is not working for me at all, even if a enter numbers >=1 the validation stops me an tell to enter de data correctly.

If the variable $GO = 0 this will be the indicator (or what some people calls flag right :?: ) to stop the user in a further if( ) that validates this 'n other things, and return him/her to correct the problem by entering the correct info. So, only if $GO = 1 the user will be allowed to pass, and this means that s/he entered only digits >0 (natural numbers).

I already did the validation at the client side (browser) using a javascript that volka passed me (thx again volka :wink: ) which only allow to type integers >=0, now is time to server validation.

Thx for ur help :D

Posted: Tue Nov 05, 2002 10:11 pm
by hob_goblin

Code: Select all

for ($i=0; $i<=9; $i++) 
&#123; 
   if (is_int($products_cant&#1111;$i]) && $products_cant&#1111;$i] >= 1) 
   &#123; $GO = TRUE; &#125; 
   else 
   &#123; $GO = FALSE; &#125; 
&#125;

Posted: Tue Nov 05, 2002 10:38 pm
by rax369
thx for ur quick reply hob_goblin, but it continues not working, even w/ur code suggestion.

Code: Select all

if ($GO == 1)
&#123;
	for ($i = 0; $i <= 9; $i++)
	&#123;
		if ( is_int($products_cant&#1111;$i]) && $products_cant&#1111;$i] >= 1 )
			&#123; $GO = 1; &#125;
		else
			&#123; $GO = 0; &#125;
	&#125;
&#125;
I just added one if ( ) before the suggestion hob_goblin gave me, due I have done some validations before this one, and ONLY[b/] if the other validations "approved the test" ($GO =1) will enter into this if ( ), could u explain me please what I'm doing wrong, coz' all it appears to be logic, but even if I type numbers >=1 I'm stoped by the validation. The problem definetely is in this piece of code. If a erase this code from the page, all goes right, but if I apply this validation then here comes the crash... 'n I need to perform this validation... what's wrong with this guys :?: :?: :cry:

ufff... sometimes I feel that PHP is so complicated, that nothing of my logic works... :x

Posted: Tue Nov 05, 2002 11:02 pm
by hob_goblin

Code: Select all

for ($i=0; $i<=9; $i++) 
&#123; 
   if (is_int(intval($products_cant&#1111;$i])) && $products_cant&#1111;$i] >= 1) 
   &#123; $GO = 1; &#125; 
   else 
   &#123; $GO = 0; &#125; 
&#125;
or post some more code so i can see if its a different problem

Posted: Tue Nov 05, 2002 11:16 pm
by rax369
hob_goblin wrote: or post some more code so i can see if its a different problem
ok, here u go, this is my complete validation code:

Code: Select all

$GO = 1;

for ($i = 0; $i <= 9; $i++)
&#123; 
	if (($products&#1111;$i] == "Choose Product / Remove Product") && ($products_cant&#1111;$i] != ""))
	&#123; $GO = 0; &#125; 

	if (($products&#1111;$i] != "Choose Product / Remove Product") && ($products_cant&#1111;$i] == ""))
	&#123; $GO = 0; &#125; 
&#125;

if ($GO == 1)
&#123;
	for ($i = 0; $i <= 9; $i++)
	&#123;
		if ( is_int($products_cant&#1111;$i]) && $products_cant&#1111;$i] >= 1 )
			&#123; $GO = 1; &#125;
		else
			&#123; $GO = 0; &#125;
	&#125;
&#125;
if ( $_POST&#1111;submit] == "Send!" AND $GO == 1)
&#123;
   //here's where my stuff is executed (db connection, calculations, etc.)
&#125;

Posted: Wed Nov 06, 2002 4:30 am
by twigletmac
From the documentation on is_int():
Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
So for validating numbers from forms you can't use is_int(). You can use is_numeric() and/or regular expressions to check that the input is an integer though.

Mac

Posted: Wed Nov 06, 2002 10:11 am
by rax369
ok, twigletmac I followed, ur advice, and I changed is_int for is_numeric, but my code, continues, not working at all. Watch how my code looks like now:

Code: Select all

$GO = 1;
for ($i = 0; $i <= 9; $i++)
&#123; 
	if (($products&#1111;$i] == "Choose Product / Remove Product") && ($products_cant&#1111;$i] != ""))
	&#123; 
		$GO = 0;
	&#125; 

	if (($products&#1111;$i] != "Choose Product / Remove Product") && ($products_cant&#1111;$i] == ""))
	&#123; 
		$GO = 0;
	&#125; 
&#125;

//here begins the problematic validation code
if ($GO == 1)
&#123;
	for ($i = 0; $i <= 9; $i++)
	&#123;
		if ( is_numeric($products_cant&#1111;$i]) && $products_cant&#1111;$i] >= 1 )
			&#123; $GO = 1; &#125;
		else
			&#123;
				$GO = 0;
				break 1;
			&#125;
	&#125;
&#125;
//ends the problematic code

// here's where it should enter if everything went ok, but it doesnt
if ( ($_POST&#1111;submit] == "Send!") && ($GO == 1) )
&#123;
   //do some calculations
&#125;
else
&#123;
   //shows a message saying that something was not entered correctly
&#125;
What's wrong w/this :?: ... the validation is very simple, $GO=0 means the user entered something that's not a number>=1 and $GO=1 means everything is correct, so... what's wrong w/this code, coz' continue not working :cry:

Posted: Thu Nov 07, 2002 2:35 am
by twigletmac
To work out where your code is going wrong you need to do some debugging and start echoing out every variable to make sure they look as expected as well as upping your error reporting to the highest level, so try something like this:

Code: Select all

&lt;?php
error_reporting (E_ALL);
echo 'DEBUG --&gt; $_POST = ';
echo '&lt;pre&gt;';
print_r($_POST);
echo '&lt;/pre&gt;';
echo 'DEBUG --&gt; $products = ';
echo '&lt;pre&gt;';
print_r($products);
echo '&lt;/pre&gt;';
echo 'DEBUG --&gt; $products_cant = ';
echo '&lt;pre&gt;';
print_r($products_cant);
echo '&lt;/pre&gt;';

$GO = 1;
echo 'DEBUG --&gt; Starting first for loop...';
for ($i = 0; $i &lt;= 9; $i++) { 
	echo 'DEBUG -&gt; $products&#1111;'.$i.'] = '.$products&#1111;$i].'&lt;br /&gt;';
	if ($products&#1111;$i] == 'Choose Product / Remove Product' &amp;&amp; !empty($products_cant&#1111;$i])) { 
	  $GO = 0; 
	} 
	if ($products&#1111;$i] != 'Choose Product / Remove Product') &amp;&amp; empty($products_cant&#1111;$i])) { 
	  $GO = 0; 
	}
	echo 'DEBUG -&gt; $GO = '.$GO.'&lt;br /&gt;';
}
echo 'DEBUG --&gt; Ending first for loop. $GO = '.$GO.'&lt;br /&gt;';

//here begins the problematic validation code
echo 'DEBUG ----&gt; Start validation.&lt;br /&gt;';
if (!empty($GO)) { 
	echo 'DEBUG --&gt; Starting for loop...&lt;br /&gt;';
	for ($i = 0; $i &lt;= 9; $i++) {
		echo 'DEBUG -&gt; $products_cant&#1111;'.$i.'] = '.$products_cant&#1111;$i].'&lt;br /&gt;';
		if (is_numeric($products_cant&#1111;$i]) &amp;&amp; $products_cant&#1111;$i] &gt;= 1 ) {
			echo 'DEBUG -&gt; $products_cant&#1111;'.$i.'] meets criteria.&lt;br /&gt;';
			$GO = 1;
		} else {
			echo 'DEBUG -&gt; $products_cant&#1111;'.$i.'] does not meet criteria.&lt;br /&gt;';
			if (is_numeric($products_cant&#1111;$i])) {
				echo 'DEBUG -&gt; $products_cant&#1111;'.$i.'] is numeric.&lt;br /&gt;';
			} else {
				echo 'DEBUG -&gt; $products_cant&#1111;'.$i.'] is not numeric.&lt;br /&gt;';
			}
			$GO = 0; 
			break 1; 
		}
		echo 'DEBUG -&gt; $GO = '.$GO.'&lt;br /&gt;';
	}
	echo 'DEBUG --&gt; Ending for loop...&lt;br /&gt;';
	echo 'DEBUG -&gt; $GO = '.$GO.'&lt;br /&gt;';
}
echo 'DEBUG ----&gt; End validation.&lt;br /&gt;';
//ends the problematic code 

// here's where it should enter if everything went ok, but it doesnt 
if ( ($_POST&#1111;'submit'] == "Send!") &amp;&amp; ($GO == 1) ) { 
   //do some calculations 
} else { 
   //shows a message saying that something was not entered correctly 
}
?&gt;
Basically echo everything out so that you can nail the problem down to a function or particular variable.

Mac

Posted: Thu Nov 07, 2002 12:57 pm
by rax369
I had already located my problem twigletmac thx for the code, is at this part: :evil:

Code: Select all

&lt;?php
//here begins the problematic validation code 
if ($GO == 1) 
{ 
   for ($i = 0; $i &lt;= 9; $i++) 
   { 
      if ( is_numeric($products_cant&#1111;$i]) &amp;&amp; $products_cant&#1111;$i] &gt;= 1 ) 
         { $GO = 1; } 
      else 
         { 
            $GO = 0; 
            break 1; 
         } 
   } 
} 
//ends the problematic code 
?&gt;
For some reason even if I enter numbers >=1 this part:

Code: Select all

&lt;?php
 if ( is_numeric($products_cant&#1111;$i]) &amp;&amp; $products_cant&#1111;$i] &gt;= 1 ) 
?&gt;
is not allowing to pass the info, coming from the text boxes.

let me show u how my form looks like, so that u r gonna have an idea (forget all that red stuff, I used this image to ask in other post something else):
Image

ok... for example, let's say a request 3 processors, of some type, 3>1 obviously, so this quantity should be allowed to go in the if I mentioned above, coz' a) is numberic and b) is >1... but it doesnt. :cry:
And because this doesnt allow the assignation $GO = 1; then this go to the else and makes $GO = 0, and here goes the problem again and again...

is_number till' I understand, takes the numeric part of text boxes and evaluate it using the conditions stablished in the if( 'here' ).... in this case the condition is.... if ( is_numeric($products_cant[$i]) && $products_cant[$i] >= 1 ) but this is not taking the numeric part coming from the form text boxes :cry:

Show me some light to correct this stupid problem please.

Thx

a little above

Posted: Thu Nov 07, 2002 1:22 pm
by phpScott
I would rethink this if statements maybe.

Code: Select all

$GO = 1; 
for ($i = 0; $i <= 9; $i++) 
&#123; 
   if (($products&#1111;$i] == "Choose Product / Remove Product") && ($products_cant&#1111;$i] != "")) 
   &#123; 
      $GO = 0; 
   &#125; 

   if (($products&#1111;$i] != "Choose Product / Remove Product") && ($products_cant&#1111;$i] == "")) 
   &#123; 
      $GO = 0; 
   &#125; 
&#125;
as to me it reads after the &&
if $products_cant[$i] is not equal to an empty string make $GO=0

which then will not go through to where you have defined your problamatic code.

That is of course I am readint this correctly.

just a thought

phpScott

Posted: Fri Nov 08, 2002 8:17 am
by rax369
thx phpScott, for ur help, I did that validation, coz' I need some piece of code that doesnt allow the user go to the 'final check page' (where the customer checks the products requested from the provider X) leaving some drop down menu without a product choosen and specifying its quantity, and viceversa... specifying the product but not the quantity to be requested, and that code worked perfectly to me.

so if everything went ok before that validation (none of the cases I mentioned happened) $GO will be equal to 1; which means "everything is ok", and therefore could enter to:

Code: Select all

&lt;?php
//here begins the problematic validation code 
if ($GO == 1)  
{  
   for ($i = 0; $i &lt;= 9; $i++)  
   {  
      if ( is_numeric($products_cant&#1111;$i]) &amp;&amp; $products_cant&#1111;$i] &gt;= 1 )  
         { $GO = 1; }  
      else  
         {  
            $GO = 0;  
            break 1;  
         }  
   }  
}  
//ends the problematic code 

?&gt;
... coz' if $GO = 1 (everything is ok) right :?:

if I would be able to make this validation and validate if the user entered a number >=1 ass well in the text boxes, show me how I could do that please, ... I have been thinking in another simple way to do that but I can figure it out how.

As I mentioned early in this post, I already allowed checked the info entered by the user in the text boxes at the client side using a javascript code that only allows the user to type digits (0, 1, 2...9), but now its time to check it at the server side.

thx for ur help guys.

Posted: Sun Nov 10, 2002 12:12 pm
by rax369
c' mon, guys, help me with this problem, I dont know what to do :cry:

Posted: Sun Nov 10, 2002 1:57 pm
by rax369
ok :!: finally I solved it, forget about the help, it's already fixed my code

:D :D :wink: