Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP form textbox validation

Posted on 2004-03-23
4
Medium Priority
?
4,004 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
[X]
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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

610 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