We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

PHP form textbox validation

mrooneyafdo
mrooneyafdo asked
on
Medium Priority
4,032 Views
Last Modified: 2012-06-27
I have a textbox on an order page. The user enters the quantity of the item that they want to buy, and then clicks an order button. What I want is a way to detect if it is an integer and at least 1.

$ValidInt = 0;
if ($quant > 0) // I was hoping any string would not pass this, though I think I am incorrect.
{
    if (is_int($quant))
    {
     $ValidInt = 1;
    }
}

That was my original attempt, but it does not seem to work. If the user enters a string or any combination of letters and numbers, then of course $ValidInt should remain 0. If it is a decimal, it should also remain zero. Finally, it must be at least 1. That is what I tried to accomplish, in a few different ways, the above seemed to be my best effort. Thanks!
Comment
Watch Question


you can also use gettype() function

why dont you do test if it is an integer first
if (is_int($quant) && ($quant>0))
{
   $validInt=1;
}

Author

Commented:
Well that is probably a much better representation of my code, but something else is still needed. That says it is invalid all the time. I think that is_int() returns false because the value in textbox is passed via the url like addtocart.php?quantity=2, so I am not sure if that automatically makes it a string or something. Whatever that does, it makes is_int() return False even for actual integers. What do I need to do?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Good enough indeed!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.