Solved

PHP form textbox validation

Posted on 2004-03-23
4
3,990 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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now