Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP form textbox validation

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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 …

829 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