PHP Regex - Value of 1 or more integers

17,760

Solution 1

Try this regexp:

/^\d+$/

The issue with your existing regexp is that it only matches strings with exactly one digit.

As for validating an int or a double:

/^\d+\.?\d*$/

Note that that regexp requires that there be at least one digit.

Solution 2

Use:

/^[0-9]+$/

The + means "one or more". Without it, your regex will only match a single digit. Or you could use the simpler variant:

/^\d+$/

For floats, try something like:

/^\d+(\.\d{1,2})?/

This will match one or more digits, optionally followed by a . and one or two digits. (i.e. .12 will not match.)

To save yourself some headaches, you can also use the is_int and is_float functions.

Lastly; note that your check is wrong. preg_match will return 0 if it fails, so you should write it as:

if (!preg_match("/^\+$/", $original_stock)) {
  // error
}

(note the !).

Solution 3

You may want to use the

is_int

Solution 4

Don't reinvent a wheel slower than an existing one, use a motorcycle: is_int.

#Assuming $original_stock is a single value...
if (is_int($original_stock)) {
    #Valid, do stuff
}
else {
    #Invalid, do stuff
}

#Assuming $original_stock is an array...
$valid = true;
foreach ($original_stock as $s) {
    if (!is_int($s)) {
        $valid = false;
        break;
    }
}
if ($valid) {...}
else {...}

Solution 5

I just ran into this exact problem and solved it this way using the regex.
I think the problem is your caret ^.

/^[0-9]$/

I moved it inside the class and got the results I needed.

function validate_int($subject)
{
    //Pattern is numbers
    //if it matches anything but numbers, we want a fail
    $pattern = '/[^0-9]/'; 
    $matches = preg_match($pattern, $subject);
    if($matches > 0)
      return false;
    else
      return true;
  }
Share:
17,760
JheeBz
Author by

JheeBz

Updated on July 18, 2022

Comments

  • JheeBz
    JheeBz almost 2 years

    Hey I'm trying to perform input validation in PHP to ensure that the stock values that are typed in are at least 1 positive integer and from 0-9. Should not contain any special characters.

    For example, any of the following values should be valid:

    7
    0
    32
    47534
    

    The following SHOULD NOT be valid:

    asdf
    35/gdf
    ../34.
    
    etc..
    

    I'm using the following if statement to check for the positive integer value of "$original_stock".

    if (preg_match("/^[0-9]$/", $original_stock)) 
    {
        $error .="Original stock must be numerical.";
    }
    

    Additionally, I have a price field which should be validated as either an int or a double.

    If there's an easier alternative to using regex, that's okay too!

    Thanks in advance :)

  • JheeBz
    JheeBz almost 13 years
    I tried the first and the second one but if the input is "30" then it causes an error either way.
  • Mat
    Mat almost 13 years
    what do you mean "causes an error"? note that these regex require that there is nothing else than the number - no whitespace in particular.
  • JheeBz
    JheeBz almost 13 years
    It does not match using the "preg_match" function and thus assigns a string to the $error variable. The number does not have any whitespace or anything by the way. It's just 30 and was taken straight from the database.
  • Mat
    Mat almost 13 years
    Edited my answer. The match works, you're not testing it correctly though.
  • Alix Axel
    Alix Axel almost 13 years
    is_int wont do it if the input comes from a form, only is_numeric. Either way, you are also validating negative integers and possibly non-integers.