PHP: Validating Numeric Values and Digits
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!
![Regular Expressions for the Rest of Us]()
Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...
![CSS @supports]()
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
![Animated AJAX Record Deletion Using Dojo]()
I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with Dojo JavaScript.
The PHP - Content & Header
The following snippet goes at the...
![CSS Circles]()
A while back I shared a clever technique for creating triangles with only CSS. Over the past year, I've found CSS triangles incredibly effective, especially when looking to create tooltips or design elements with a likewise pointer pattern. There's another common shape...
Why don’t you use ctype_digit() instead of this preg_match()? It’s faster =)
\d is shorthand for [0-9]
@Mark: Damnit Sanborn — I’m going to make you write a regex tutorial for me! :)
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.
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.
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…
Why would you create a new function that returns is_numeric, given that your new function name is longer than typing ‘is_numeric’ anyway?
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()
Josh, you can use the ctype php function :)
(why my older post were deleted?)
Actually you can use is_int() if your using the is_* functions. However Alex is right and the ctype functions are better.
to check numeric variable and length of variable = 4 numbers:
exemple :
You can remplace {4} by {1,4} to check if variable is between 1 and 4 numbers
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.
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.
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 thisI realize it is kind of off topic but if you combine
strpos
for'.'
andis_numeric()
it should work the same.EDIT: grr_group_year should be theyear. I missed that when I stripped my own code out of my example.