?
Solved

PHP form textbox validation

Posted on 2004-03-23
4
Medium Priority
?
4,011 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!
0
Comment
Question by:mrooneyafdo
  • 2
4 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10662006

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;
}
0
 

Author Comment

by:mrooneyafdo
ID: 10662105
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?
0
 
LVL 3

Accepted Solution

by:
Drift3r earned 800 total points
ID: 10662736
You'll have to use the is_numeric also ...

http://uk2.php.net/manual/en/function.is-numeric.php

Check the user comments ... there are many ways to do that and problems with users trying to input something like 4e4 which is numeric ...

This was taken from there:

-------------------------------------------------------
One solution
-------------------------------------------------------

<?php
/* This function is not useful if you want
to check that someone has filled in only
numbers into a form because for example
4e4 and 444 are both "numeric".

I used a regular expression for this problem
and it works pretty good. Maybe it is a good
idea to write a function and then to use it.

$input_number = "444"; // Answer 1
$input_number = "44 "; // Answer 2
$input_number = "4 4"; // Answer 2
$input_number = "4e4"; // Answer 2
$input_number = "e44"; // Answer 2
$input_number = "e4e"; // Answer 2
$input_number = "abc"; // Answer 2
*/
$input_number = "444";

if (preg_match ("/^([0-9]+)$/", $input_number)) {
     print "Answer 1";
} else {
     print "Answer 2";
}
?>

Note: You could do something like: Not number? $input_number=null
-------------------------------------------------------
Another:
-------------------------------------------------------

This is a little more explicit and won't break when the value can't be legally cast as an int

function is_intValued($var)
{
   // Determines if a variable's value is an integer regardless of type
   // meant to be an analogy to PHP's is_numeric()
   if (is_int($var)) return TRUE;
   if (is_string($var) and $var === (string)(int) $var) return TRUE;
   if (is_float($var) and $var === (float)(int) $var) return TRUE;
   else return FALSE;
}

Note: Not as good as it doesn't fix big numbers: 4e4

Again ... this examples were taken from the is_numeric php documentation page.
But the ideia is this: the arguments are always passed like a string so somewhere you'll have to cast it back to int ... or use a regular expression to check if the string only has digits like "0123456789".

Good enough? ;)

Drift3r

0
 

Author Comment

by:mrooneyafdo
ID: 10662813
Good enough indeed!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question