Reputation: 2848
Problem
I need to validate some input. It should accept positive integers only. So far, my code is not working properly.
My code
if ( !is_numeric($data['dollar']) {
return FALSE;
}
Examples
0 // TRUE
1 // TRUE
-1 // FALSE
0.9 // FALSE
Upvotes: 2
Views: 775
Reputation: 39414
You can use the validation functions:
$type = FILTER_VALIDATE_INT;
$options = [ 'min_range' => 1, 'max_range' => PHP_INT_MAX ];
if (false === filter_var($data['dollar'], $type, [ 'options' => $options ])) {
return FALSE;
}
Upvotes: 0
Reputation: 121
You can use regex below to solve your problem
return preg_match("/^[0-9]*$/",$data['dollar']);
Upvotes: -1