Source for file CA.php
Documentation is available at CA.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
* Specific validation methods for data used in Canada
* This source file is subject to the New BSD license, That is bundled
* with this package in the file LICENSE, and is available through
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the new BSDlicense and are unable
* to obtain it through the world-wide-web, please send a note to
* pajoye@php.net so we can mail you a copy immediately.
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 1997-2005 Philippe Jausions
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: CA.php,v 1.12 2008/09/01 17:17:41 jausions Exp $
* @link http://pear.php.net/package/Validate_CA
* Data validation class for Canada
* This class provides methods to validate:
* - Social Insurance Number (aka SIN)
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 1997-2005 Philippe Jausions
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate_CA
* Validates a number according to Luhn check algorithm
* This function checks given number according Luhn check
* algorithm. It is published on several places, see links.
* @param string $number number to check
* @return bool TRUE if number is valid, FALSE otherwise
* @deprecated Scheduled for removal before beta release
* @link http://www.webopedia.com/TERM/L/Luhn_formula.html
* @link http://www.merriampark.com/anatomycc.htm
* @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
* @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
* Validates a number according to Luhn check algorithm
* This function checks given number according Luhn check
* algorithm. It is published on several places, see links:
* @param string $number number to check
* @return bool TRUE if number is valid, FALSE otherwise
* @link http://www.webopedia.com/TERM/L/Luhn_formula.html
* @link http://www.merriampark.com/anatomycc.htm
* @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
* @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
$len_number = strlen($number);
for ($k = $len_number % 2; $k < $len_number; $k += 2 ) {
if ((intval($number{$k}) * 2 ) > 9 ) {
$sum += (intval($number{$k}) * 2 ) - 9;
$sum += intval($number{$k}) * 2;
for ($k = ($len_number % 2 ) ^ 1; $k < $len_number; $k += 2 ) {
return ($sum % 10 ) ? false : true;
* Validates a Canadian social insurance number (SIN)
* For unification between country-based validation packages,
* this method is named ssn()
* @param string $ssn number to validate
* @param int $expiryDate expiry date for SIN starting
* with a 9 (UNIX timestamp)
* @link http://www.hrsdc.gc.ca/en/hip/lld/cesg/promotersection/files/Interface_Transaction_Standards_V301_English.pdf
function ssn($ssn, $expiryDate = null )
// remove any dashes, spaces, returns, tabs or slashes
if (($len = strlen($ssn)) != 9
|| strspn($ssn, '0123456789') != $len
|| ($ssn{0 } == '9' && $expiryDate <= time())
|| $ssn{0 } == '0' || $ssn{0 } == '3' || $ssn{0 } == '8') {
* Validates a Canadian Postal Code
* @param string $postalCode the postal code to validate
* @param string $province the province code
* @return boolean TRUE if code is valid, FALSE otherwise
* @link www.canadapost.ca/business/tools/pg/preparation/mpp2-04-e.asp#c154
$letters = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
$sRegExp = " [ABCEGHJKLMNPRSTVXY][0-9][$letters]"
. " [ \t-]*[0-9][$letters][0-9]";
case 'NL': // Newfoundland and Labrador
case 'NF': // Newfoundland (kept for BC)
case 'NS': // Nova Scotia
case 'PE': // Prince Edward Island
case 'NB': // New Brunswick
case 'SK': // Saskatchewan
case 'BC': // British Columbia
case 'NT': // Northwest Territories
case 'YK': // Yukon Territory
case 'YT': // Yukon Territory (Canada Post)
$sRegExp .= " [0-9][$letters][ \t-]*[0-9][ $letters][0-9]";
$sRegExp = '/^' . $sRegExp . '$/';
* Validates a "region" (i.e. province) code
* @param string $region 2-letter province code
* @return bool Whether the code is a valid province
case 'NF': // Newfoundland (kept for BC)
case 'YT': // Yukon (Canada Post)
* Validates a Canadian phone number.
* 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, dashes.
* THIS SHOULD EVENTUALLY take a FORMAT in the options, instead
* @param string $number phone to validate
* @param bool $withAreaCode require the area code?
* @return bool Whether the phone number is valid.
// just seven digits, maybe a space or dash
return (boolean) preg_match('/^[2-9]\d{2}[- ]?\d{4}$/', $number);
// ten digits, maybe spaces and/or dashes and/or parentheses
$reg = '/^[0-1]?[- ]?(\()?[2-9]\d{2}(?(1)\))[- ]?[2-9]\d{2}[- ]?\d{4}$/';
Documentation generated on Mon, 01 Sep 2008 13:30:03 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|