Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

PHP: Validating Numeric Values and Digits

12 Responses »

I've inherited a lot of code where the previous developer used PHP's is_numeric() and it's gotten them into trouble. There's a big difference between validating that a value is numeric and validating that a value is all digits. Here's how to validate the two:

/* numeric, decimal passes */
function validate_numeric($variable) {
	return is_numeric($variable);
}

/* digits only, no dots */
function is_digits($element) {
	return !preg_match ("/[^0-9]/", $element);
}

A customer number, for example, should not have any decimal points but using is_numeric() would let a decimal value pass the test. Don't be this guy!

Discussion

  1. November 21, 2008 @ 11:36 am

    Why don’t you use ctype_digit() instead of this preg_match()? It’s faster =)

  2. November 21, 2008 @ 11:38 am

    \d is shorthand for [0-9]

  3. November 21, 2008 @ 12:06 pm

    @Mark: Damnit Sanborn — I’m going to make you write a regex tutorial for me! :)

  4. November 21, 2008 @ 12:10 pm

    why not just check if it’s an integer and if it’s a string of numbers just check if intval($num) == $num ? I tend to stray away from regex if possible.

  5. brian
    November 21, 2008 @ 1:05 pm

    You could also use PHP’s built-in function ctype_digit() which will return true only if all characters in the value it is passed are numeric. i.e. 1234 would return true, 1,234 returns false, 1.234 returns false.

  6. November 21, 2008 @ 1:25 pm

    I have definitely have seen people misuse the is_numeric function as well.

    @david – a regex tutorial would be great. You can always learn something new…

  7. rikki
    November 21, 2008 @ 2:16 pm

    Why would you create a new function that returns is_numeric, given that your new function name is longer than typing ‘is_numeric’ anyway?

  8. November 21, 2008 @ 8:01 pm

    You can use is_int() to find out if it’s an integer instead of a regexp or having to compare using intval() (as suggested above). The only catch with this is that is must fit within the integer type range so you may still need to use a regexp if it’s a really long number.

    And I’m a little confused why you’d wrap is_numeric() inside another function call when all the wrapper function does is return the value from is_numeric()

  9. alex
    November 22, 2008 @ 8:32 am

    Josh, you can use the ctype php function :)

    (why my older post were deleted?)

  10. david
    November 22, 2008 @ 9:31 am

    Actually you can use is_int() if your using the is_* functions. However Alex is right and the ctype functions are better.

  11. elkalidi abdelkader
    November 23, 2008 @ 7:36 pm

    to check numeric variable and lenth of variable = 4 numbers:

    preg_match(“#^[0-9]{4}$#”,$variable)

    exemple :
    $year = preg_match(“#^[0-9]{4}$#”,$variable) ? $variable : date(“Y”);

    You can remplace {4} by {1,4} to check if variable is between 1 and 4 numbers

  12. endryou
    December 16, 2008 @ 7:45 am

    One important detail about ctype_digit: it accepts string as parameter.
    For example:
    ctype_digit(’5′) === true, but ctype_digit(5) === false.
    So it’s safer to write ctype_digit((string)$my_fancy_var) but again not enough for nulls,
    booleans, objects or resources.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!