O'Reilly

PHP: Validating Numeric Values and Digits

By on  

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!

Track.js Error Reporting

Recent Features

  • 9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

Incredible Demos

  • Image Data URIs with PHP

    If you troll page markup like me, you've no doubt seen the use of data URI's within image src attributes. Instead of providing a traditional address to the image, the image file data is base64-encoded and stuffed within the src attribute. Doing so saves...

  • Get Slick with MooTools Kwicks

    When I first saw MooTools graphical navigation, I was impressed. I thought it was a very simple yet creative way of using Flash. When I right-clicked and saw that it was JavaScript, I was floored. How could they achieve such...

Discussion

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

  2. \d is shorthand for [0-9]

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

  4. 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

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

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

    Josh, you can use the ctype php function :)

    (why my older post were deleted?)

  10. David

    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

    to check numeric variable and length 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

    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.

  13. Doesn’t ‘/[0-9]+/’ equate to contains or or more consecutive digits. For all, I think you probably need ‘/^[0-9]+$/’ or more succinctly ‘/^\d+$/’.

    Kudos to Elkalidi Abdelkader for using them but it doesn’t hurt to be explicit.

  14. For those interested in finding a valid date, I found an issue where PHP errors would be thrown with Checkdate(m,d,y) so I did this

    if (empty($_POST['theyear'])) { //For me this was optional so for validating I allowed the field to be blank...
    		$yearcheck = '2000'; //...but would need to still enter a year into the check (any year will work but I picked one that included leap years)
    		}else//there is a data in the year field
    		if (is_numeric($_POST['theyear']) //Is it a number?
    		and strpos('.', $_POST['theyear'])==false //Is there a decimal in the number?
    		and strlen($_POST['theyear'])==4) { //Is the number 4 digits long?
    		$yearcheck = $_POST['theyear']; //Passed the test, checked this date
    		}else //didn't pass the test so....
    		$yearcheck = '0000'; //this year will always be valid on the PHP side BUT will cause checkdate() to always return false.
    		if (checkdate($_POST['themonth'], $_POST['thedate'], $yearcheck)==false //checks if the month date year combination is right...
    		and !empty($_POST['grr_group_year'])) //...but only if we are entering a date to begin with
    		$messages[] = __('Date is not valid!', 'my_page'); //My custom warning (at first I had it return the little poem for the number of dates in a month)
    

    I realize it is kind of off topic but if you combine strpos for ‘.’ and is_numeric() it should work the same.

  15. EDIT: grr_group_year should be theyear. I missed that when I stripped my own code out of my example.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • GSAP + SVG For Power Users: Motion Along A Path

    Now that the GreenSock API is picking up steam, there are many tutorials and Getting Started guides out there to provide good introductions to the library, not to mention GreenSock’s own Forum and Documentation. This article isn’t intended for beginners, but rather a...

  • Copy a Directory from Command Line

    Copying a directory for the sake of backup is something I do often, especially when I'm trying to figure out why something isn't working when I use an external library.  I'll copy the directory structure as a backup, mess around with the original source until I find a solution,...

  • Hotjar &#8211; All-in-one Analytics and Feedback

    Website analytics are a massive business -- the more data you can collect with regard to your users' behaviors on your site, the more you can increase and maximise conversion...and increased conversion is always good.  Sometimes increase conversion means more money, improved user experience, viewer retention,...

  • Crafting a 3D React Carousel

    There is something in me that is amazed but beautiful 3D interfaces. And it doesn’t matter whether they’re functional like Gyroscope features menu, technology demonstrators like the amazing periodic table demo from famous or they’re artistic representation pushing the limits of...

  • Collaborative Editing in JavaScript: An Intro to Operational Transformation

    I've set out to build a robust collaborative code editor for the web. It’s called Codr, and it lets developers work together in real time - like Google Docs for code. For web developers, Codr doubles as a shared reactive work surface where every change is...