PEAR is archived and read-only

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

Home » Validate » Validate_CA » Manual

This package is in alpha state Package contains locale validation for Canada such as: Social Inurance Numbers (SIN) Regions (Provinces) Postal Codes Phone Numbers See also: the api documentation The page on pear.php.net

Introduction

Introduction – Introduction to validate_CA

Description

The package offers some methods to validate specific data for Canada

Usage

General usage

Every module of Validate follows the same philosophy. Propose some validation method retuning boolean result. Some methods have some optional parameters to set a stronger check.

sample

<?php

// Include the package
require_once 'Validate/CA.php';
?>

List of aivailable validations

List of aivailable validations – available validation in validate_ca

Validate a canadian social security number

The canadian social security number is on the SIS card of all canadian.

A check digit is the last one, computed the standard _get_control_number function.

sample

<?php

// Include the package
require_once('Validate/CA.php');

$badSsn = '012345674';
$result = Validate_CA::ssn($badSsn);
echo 'Test ' . $badSsn .' : <br />';
var_export($result);

echo '<br /><br />';
$goodSsn = '123456782';
$result = Validate_CA::ssn($goodSsn);
echo 'Test ' . $goodSsn .' : <br />';
var_export($result);
?>

Output :

     
Test 012345674 :
false

Test 123456782 :
true
     
    

Validate a canadian postcode

The postal code is a six-character, uniformly structured alphanumeric code in the form of ANA NAN where "A" represents an alphabetic character and "N" represents a numeric character.

The postal code is made up of two segments:

The first, the Forward Sortation Area (FSA), is a combination of three characters (alpha - numeric - alpha).

It identifies a major geographic area in an urban or a rural location.

The third character of the FSA segment (M4B), in conjunction with the first two characters, describes an exact area of a city or town or other geographic area.

The second segment, Local Delivery Unit (LDU), is a combination of three characters (numeric - alpha - numeric).

It identifies the smallest delivery unit within a forward sortation area.

The LDU, identified by the last three characters of the postal code, allows for a final sort within an FSA.

In Urban Areas, the last three digits may indicate a specific city block (one side of a street between two intersecting streets), a single building or, in some cases, a large volume mail receiver.

In Rural Areas, the last three digits (LDU), together with the FSA, identify a specific Rural community.

First parameter of the method is the post code to validate.

An optional parameter for limite the request to a province.

sample

<?php

// Include the package
require_once('Validate/CA.php');

$badPostCode = '48103';
$result = Validate_CA::postalCode($badPostCode);
echo 'Test ' . $badPostCode .' : <br />';
var_export($result);

echo '<br /><br />';
$goodPostCode = 'H2M 2J1';
$result = Validate_CA::postalCode($goodPostCode);
echo 'Test ' . $goodPostCode .' : <br />';
var_export($result);
?>

Output :

     
Test 48103 :
false

Test H2M 2J1 :
true
     
    

sample using province parameter

H2M 2J1 like a good post code, but province don't exit.

<?php

// Include the package
require_once('Validate/CA.php');

$postalCode = 'H2M 2J1'; // in Montreal area

$result = Validate_CA::postalCode($postalCode);
echo 'Test ' . $postalCode .' : <br />';
var_export($result);

echo '<br /><br />';
$result = Validate_CA::postalCode($postalCode,'QC');
// QC for Montreal
echo 'Test ' . $postalCode .' in QC: <br />';
var_export($result);

echo '<br /><br />';
$result = Validate_CA::postalCode($postalCode,'AB');
// AB for Toronto
echo 'Test ' . $postalCode .' in AB: <br />';
var_export($result);

?>

Output :

     
Test H2M 2J1 :
true

Test H2M 2J1 in QC:
true

Test H2M 2J1 in AB:
false
     
    

Validate a phonenumber

Canada and the United States share the same numbering plan, hence you can also call Validate_US::phoneNumber()

Can allow only seven digit numbers.

Also allows the formats, (xxx) xxx-xxxx, xxx xxx-xxxx, and now x (xxx) xxx-xxxx or various combination without spaces or dashes.

sample

<?php
// Include the package
require_once('Validate/CA.php');

$phoneNumber = '467875098x';
$result = Validate_CA::phoneNumber($phoneNumber);
echo 'Test ' . $phoneNumber .' : <br />';
var_export($result);
echo '<br />';

$phoneNumber = '4678750987';
$result = Validate_CA::phoneNumber($phoneNumber);
echo 'Test ' . $phoneNumber .' : <br />';
var_export($result);

?>

Output :

     
Test 467875098x :
false
Test 4678750987 :
true
     
    

sample

See now with the parameter

<?php
// Include the package
require_once('Validate/CA.php');

$phoneNumber = '8750987';
$result = Validate_CA::phoneNumber($phoneNumber,false);
echo 'Test ' . $phoneNumber .' : <br />';
var_export($result);

echo '<br /><br />';
$phoneNumber = '8750987';
echo 'Test ' . $phoneNumber .' : <br />';
echo 'With $requireAreaCode false <br />';
$result = Validate_CA::phoneNumber($phoneNumber,false);
var_export($result);
echo '<br />';
echo 'With $requireAreaCode true<br />';
$result = Validate_CA::phoneNumber($phoneNumber,true);
var_export($result);


echo '<br /><br />';
$phoneNumber = '(467)8750987';
echo 'Test ' . $phoneNumber .' : <br />';
echo 'With $requireAreaCode false <br />';
$result = Validate_CA::phoneNumber($phoneNumber,false);
var_export($result);
echo '<br />';
echo 'With $requireAreaCode true<br />';
$result = Validate_CA::phoneNumber($phoneNumber,true);
var_export($result);


?>

Output :

     
Test 8750987 :
true

Test 8750987 :
With $requireAreaCode false
true
With $requireAreaCode true
false

Test (467)8750987 :
With $requireAreaCode false
false
With $requireAreaCode true
true