PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

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:

Various option are:

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:

Various option are:

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:

Various option are:

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!';
}
?>