10

I need to be able to see if a form input in PHP is numeric. If it is not numeric, the website should redirect. I have tried is_numeric() but it does not seem to work.

Code examples will be nice.

I am developing a shopping cart that accepts an integer value for the quantity. I am trying this:

if(!is_numeric($quantity)){
                //redirect($data['referurl']."/badinput");
                echo "is not numeric";
        }
1
  • please poste some code so we can understand why is_numeric() is not working for you. Commented Nov 25, 2008 at 16:22

9 Answers 9

14
if(!is_numeric($quantity == 0)){
                //redirect($data['referurl']."/badinput");
                echo "is not numeric";

What you have here are two nested conditions. Let's say $quantity is 1.

The first condition evaluates 1 == 0 and returns FALSE. The second condition checks if FALSE is numeric and returns FALSE because FALSE is not numeric.

just write:

if (!is_numeric($quantity))
{
    echo 'is not numeric';
}
Sign up to request clarification or add additional context in comments.

Comments

7

You should probably explain what you mean by "numeric" - integral, floating point, exponential notation etc? is_numeric() will accept all of these.

If you want to check that a string contains nothing other than digits, then you could use a regular expression, e.g.

/^\d+$/

If you're going to use the actual value as if it were an integer, you'll probably want to pass it through intval() anyway, which will return 0 if the value cannot be parsed - if 0 is a valid value, then you'll probably have to handle that in some way, maybe by constraining the lower range of the value.

1 Comment

There's a small error in your regex. Should be /^\d+$/ You need to escape the d.
2

It might also be wise to do some client side validation of the input using JavaScript.

The round-trip to the server and back is a long one for what might amount to a typo, and you'll reduce server overhead by making the client browser do a bit of the quality assurance beforehand.

1 Comment

Very late comment here, but please if you do the above, do this as well as server side validation for security reasons.
1

Check is_int and is_numeric. There are examples in each of the links. If you need more help, I would post the data you are having problems with and a code sample.

EDIT:

$quantity == 0

will always be numeric, since it will return a boolean value (1 or 0). The correct thing to do it:

if ( is_numeric( $quantity ) ) {
...
}

or

if ( is_int( $quantity ) ) {
...
}

Comments

1

Another alternative is ctype_digit. From the docs:

Checks if all of the characters in the provided string, text, are numerical. Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.

<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
    if (ctype_digit($testcase)) {
        echo "The string $testcase consists of all digits.\n";
    } else {
        echo "The string $testcase does not consist of all digits.\n";
    }
}
?>

The above example will output:
The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.

<?php

$numeric_string = '42';
$integer        = 42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false (ASCII 42 is the * character)

is_numeric($numeric_string);   // true
is_numeric($integer);          // true
?>

Comments

0

What Rob said, although instead of regular expressions to check for digits, I would use ctype_digit

Comments

0

tharkun has the best answer so far. If you're asking a question like this, it's my guess that you don't really want to start messing around with reg-exp's just yet.

Re-think your logic. Why do you want to check to see if $quantity==0 is a numeric result? If you're trying to avoid errors b/c you think it's possible for quantity to not have an assigned value, you're checking a little late. This is a very common (and nefarious) security hole in your application -- if $quantity has a value derived at all from user input, please make sure to sanitize the input before it reaches this point in execution. As a smaller part of the issue, you will not need to assign a default value, because you sanitized your input previously (and that sanitization is where you'd deal with a 'no input' situation).

Good luck!

1 Comment

Yeah, the entire problem was from a typo. Silly me
0

Use regular expressions: /^\d+$/ should solve the problem

Comments

0

Try this:

$numeric = "1"; //true default

$string = trim($_GET['string']);

$chkarray = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ",", ".");

for ($i=0; $i < strlen($string); $i++) { 
    if (!in_array(substr($string, $i, 1), $chkarray)) {
        $numeric = "0";
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.