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!
/* 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!
0 comments:
Post a Comment