Home » Validate » Validate » Manual
The main package provides methods to validate various data. It includes : numbers (min/max, decimal or not) email (syntax, domain check) string (predifined type alpha upper and/or lowercase, numeric,...) date (date, options) - Options can be format (Format of the date (%d-%m-%Y) or rfc822_compliant), min (The date has to be greater than this array ($day, $month, $year) or PEAR::Date object), max (The date hast to be smaller than this array ($day, $month, $year) or PEAR::Date object) uri (RFC2396) possibility valid multiple data with a single method call (::multiple) See also : the api documentation The page on pear.php.net
Introduction
Introduction – Introduction to Validate
Overview
With this package, one can easily validate various data. It includes numbers, email, string, date, URI and posibility valid multiple data with a single method call.
Some of above mentioned features have corresponding RFCs, in case, Validate conforms to those RFCs. For example email validation mostly covers RFC 822 or URI conforms to RFC 2396.
A few examples
Validating an email address
<?php
require_once 'Validate.php';
if (!Validate::email('johndoe@example.net')) {
echo 'Invalid Email address';
} else {
echo 'Its Valid!';
}
?>
Validating dates
In this example, date would first checked against desired format, and then get checked to see if it's not before "15 February 1986" or after "23 August 2008".
<?php
require_once 'Validate.php';
var_dump(
Validate::date(
'121202',
array(
'format' => '%d%m%y',
'min' => array('15', '02', '1986'),
'max' => array('23', '08', '2008')
)
)
);
?>
Email address validation
Email address validation – Validation for email addresses.
Introduction to Email Address validation
This method validate email addresses against both the general format and the one described in RFC 822.
This method takes two arguments:
- An email address.
- An array of options (optional).
Various option are:
-
check_domain(boolean) - Check or not if the domain exists. -
use_rfc822(boolean) - Apply the full RFC822 grammar. -
fullTLDValidation(boolean) - All top-level domains. -
VALIDATE_GTLD_EMAILS(boolean) - Only generic top-level domains. -
VALIDATE_CCTLD_EMAILS(boolean) - Only country code top-level domains. -
VALIDATE_ITLD_EMAILS(boolean) - Only international top-level domains.
How to use Email Validation
Email Validation
The following example assumes that one wants to use RFC822 strict mode to validate email addresses.
<?php
require_once 'Validate.php';
$email = '"Doe, John" <johndoe@example.net>';
if (Validate::email($email, array('use_rfc822' => true))) {
echo 'Valid!';
} else {
echo $email . ' failed.';
}
?>
And the following one assumes that one wants to check if the domain exists.
<?php
require_once 'Validate.php';
$email = 'info@example.com';
if (Validate::email($email, array('check_domain' => 'true'))) {
echo $email . ' is valid and domain exists';
}
?>
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' => -7 ))) {
echo 'Valid';
} else {
echo 'Invalid';
}
?>
String validation
String validation – Validation for strings.
Introduction to String validation
This method validates string using the given format.
This method takes two arguments:
- String.
- An array of options.
Various option are:
-
format(mixed) - Format of the string-
VALIDATE_NUM- Number (0-9). -
VALIDATE_SPACE- Space (\s). -
VALIDATE_ALPHA_LOWER- Lower case alphabets (a-z). -
VALIDATE_ALPHA_UPPER- Upper case alphabets (A-Z). -
VALIDATE_ALPHA- Alphabets (a-Z). -
VALIDATE_EALPHA_LOWER- Lower case letters with an accent (French, ...), umlauts (German), special characters (e.g. Skandinavian) andVALIDATE_ALPHA_LOWER. -
VALIDATE_EALPHA_UPPER- Upper case letters with an accent (French, ...), umlauts (German), special characters (e.g. Skandinavian) andVALIDATE_ALPHA_UPPER. -
VALIDATE_EALPHA- Letters with an accent (French, ...), umlauts (German), special characters (e.g. Skandinavian) andVALIDATE_ALPHA. -
VALIDATE_PUNCTUATION- Punctuation .,;:&"?!', "(" and ")". -
VALIDATE_NAME-VALIDATE_EALPHA,VALIDATE_SPACE, "'" and "-". -
VALIDATE_STREET-VALIDATE_NUMandVALIDATE_NAME, "\./", "º" and "ª".
-
-
min_length(int) - Minimum length. -
max_length(int) - Maximum length.
How to use String Validation
String Validation
The following example assumes that one wants to validate a string when only uppercase alphabets, numbers and space are allowed
<?php
require_once 'Validate.php';
if (Validate::string("1984 GEORGE ORWELL", array(
'format' => VALIDATE_NUM . VALIDATE_SPACE . VALIDATE_ALPHA_UPPER))) {
echo 'Valid!';
} else {
echo 'Invalid!';
}
?>