Number validation

Number validation – Validation for numbers.

Introduction to Number validation

This method validates numbers. Decimal or not, max and min.

This method takes two arguments:

  • A number.
  • An array of options (optional).

Various option are:

  • decimal (mixed) - Decimal chars or false when decimal not allowed. For example ",." to allow both "." and ",".
  • dec_prec (int) - Number of allowed decimals.
  • min (float) - Minimum value.
  • max (float) - Maximum value.

How to use Number Validation

Number Validation

The following example assumes that one wants to validate a number when decimals are allowed and decimal character is ".", and number of allowed decimals is 4.

<?php
require_once 'Validate.php';

if (
Validate::number(8.0004, array('decimal' => '.''dec_prec' => 4))) {
    echo 
'Valid number';
}
?>

And the following one assumes that one wants to validate a decimal number with decimal character "," or "." while it's less than "-7" and greater than "-9".

<?php
require_once 'Validate.php';

$number '-8,1';
if (
Validate::number($number,
    array(
'decimal' => '.,''min' => -9'max' => -))) {
    echo 
'Valid';
} else {
    echo 
'Invalid';
}
?>
Validation for email addresses. (Previous) Validation for strings. (Next)
Last updated: Wed, 23 Apr 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.