Skip to main content
Version: Fegg 5.x (next)

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​

NameData typeDefaultRemarks
$namestringField name (key of the error message)
$valuemixedValue to check
$codemixed''Error code, array of error code and parameters, or error message (see Configuration)
$decimalPointFlagbooleanfalsetrue: allow a decimal part
$minusFlagbooleanfalsetrue: 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