Validation::numeric
Check numeric or not
Description
public boolean Validation::numeric( string $name, mixed $value, [ mixed $code = '', boolean $decimalPointFlag = false, boolean $minusFlag = false ] )
Valid when the value contains only the digits 0-9. $decimalPointFlag also allows a decimal part (12.5) and $minusFlag a leading -. Use the error codes @numeric_decimalpoint and @numeric_minus for the messages of those cases.
An empty value ('', null, '0', whitespace only) is not checked and returns true: combine with Validation::required when the field is required. When $name already has an error, the check is skipped and returns false, so only the first error of each field is kept.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $name | string | Field name (key of the error message) | |
| $value | mixed | Value to check | |
| $code | mixed | '' | Error code, array of error code and parameters, or error message (see Configuration) |
| $decimalPointFlag | boolean | false | true: allow a decimal part |
| $minusFlag | boolean | false | true: allow a leading - |
Example
Controller
use Fegg\Validation;
$validation = new Validation();
$validation->numeric('success', '123', array('@numeric', 'Quantity')); // -> true
$validation->numeric('failed', '12.5', array('@numeric', 'Quantity')); // -> false
var_dump($validation->getErrorMessage());
// -> array('failed' => '<strong>Quantity</strong> は 半角数字で入力してください')
// Decimals
$validation->numeric('price', '12.5', array('@numeric_decimalpoint', 'Price'), true); // -> true
// Negative numbers
$validation->numeric('offset', '-10', array('@numeric_minus', 'Offset'), false, true); // -> true