Skip to main content
Version: Fegg 5.x

Validation::numeric

Check that a value is a number

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.

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\Tool\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