6

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
  • 303
  • 1
  • 4
  • 14

5 Answers5

10

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.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
4

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 !).

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    I tried the first and the second one but if the input is "30" then it causes an error either way. – JheeBz May 29 '11 at 06:50
  • what do you mean "causes an error"? note that these regex require that there is nothing else than the number - no whitespace in particular. – Mat May 29 '11 at 06:52
  • 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. – JheeBz May 29 '11 at 07:00
  • Edited my answer. The match works, you're not testing it correctly though. – Mat May 29 '11 at 07:09
1

You may want to use the

is_int
luca
  • 36,606
  • 27
  • 86
  • 125
1

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 {...}
Zirak
  • 38,920
  • 13
  • 81
  • 92
  • `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. – Alix Axel May 29 '11 at 08:29
1

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;
  }
Surren D
  • 11
  • 1