Validate_NZ
[ class tree: Validate_NZ ] [ index: Validate_NZ ] [ all elements ]

Source for file NZ.php

Documentation is available at NZ.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2006 Byron Adams                                     |
  5. // +----------------------------------------------------------------------+
  6. // | This source file is subject to the New BSD license, That is bundled  |
  7. // | with this package in the file LICENSE, and is available through      |
  8. // | the world-wide-web at                                                |
  9. // | http://www.opensource.org/licenses/bsd-license.php                   |
  10. // | If you did not receive a copy of the new BSDlicense and are unable   |
  11. // | to obtain it through the world-wide-web, please send a note to       |
  12. // | pajoye@php.net so we can mail you a copy immediately.                |
  13. // +----------------------------------------------------------------------+
  14. // | Author: Byron Adams <Byron.adams54@gmail.com>                        |
  15. // +----------------------------------------------------------------------+
  16. //
  17. /**
  18.  *  Data validation class for New Zealand
  19.  *
  20.  * This class provides methods to validate:
  21.  *  - IRD numbers
  22.  *  - Regional codes
  23.  *  - Telephone number
  24.  *  - Postal code
  25.  *  - Bank AC
  26.  *
  27.  * @category   Validate
  28.  * @package    Validate_NZ
  29.  * @author     Byron Adams <Byron.adams54@gmail.com>
  30.  * @copyright  (c) 2006 Byron Adams
  31.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  32.  */
  33.  
  34. {
  35.     /**
  36.      * Validate  New Zealand postal codes
  37.      *
  38.      * @static
  39.      * @access   public
  40.      * @param    string  $postcode,  postcode to validate
  41.      * @param    bool    $strong,    optional; strong checks against a list of postcodes
  42.      * @return   bool 
  43.      * @link      http://www.nzpost.co.nz/nzpost/images/addressing.nzpost/pdfs/postcodedirectory_nomaps.pdf
  44.      */
  45.     function postalCode($postcode,$strong = true)
  46.     {
  47.         if ($strong{
  48.             static $postcodes;
  49.  
  50.             $postcodes = array(
  51.                                   "0110","0420","0310","1010","0610","0600","2012","2105",
  52.                                   "0505","1081","1022","2102","2010","2022","2013","0630",
  53.                                   "0614","0612","2014","1025","0931","3210","3214","3204",
  54.                                   "3200","3410","3118","3112","3015","4130","4122","4110",
  55.                                   "4312","4501","4500","4310","4825","4820","5032","5024",
  56.                                   "5510","4410","5036","5018","6022","5010","6011","6037",
  57.                                   "5028","5034","7281","7173","7196","7073","7183","7005",
  58.                                   "7007","7022","7025","7071","7072","7077","7081","7091",
  59.                                   "7095","7096","7220","7110","7120","7282","7284","7175",
  60.                                   "7182","7194","7192","7193","7195","7197","7198","7204",
  61.                                   "7210","7100","7271","7272","7273","7274","7275","7276",
  62.                                   "7285","7178","7201","7010","7011","7020","8013","8041",
  63.                                   "7334","8011","7402","8022","8083","8062","7999","8024",
  64.                                   "8053","8051","7832","7300","7802","8042","8025","8081",
  65.                                   "7610","7825","8014","9810","9016","9010","9822","9400",
  66.                                   "9012","9014","9022","9013","9023","9401","9812");
  67.  
  68.                return in_array((int)$postcode$postcodes);
  69.         }
  70.         return (bool)ereg('^[0-9]{4}$'$postcode);
  71.     }
  72.  
  73.     /**
  74.      * Validates a New Zealand IRD Number (ssn equiv)
  75.      *
  76.      * recently the format has changed to having a
  77.      * prefix of 0, this will work with both new and old IRD numbers.
  78.      *
  79.      * @param string     $ssn;  IRD number to validate
  80.      * @returns bool
  81.      */
  82.     function ssn($ssn)
  83.     {
  84.         $ssn str_replace(array("-"," "),'',trim($ssn));
  85.  
  86.         //if its a new IRD chop the first character off.
  87.         if ($ssn{0=="0" && strlen($ssn)==9{
  88.             $ssn substr($ssn,1,strlen($ssn));
  89.         }
  90.  
  91.         if (preg_match("(^[0-9]{8}$)",$ssn)) {
  92.             return true;
  93.         }
  94.  
  95.         return false;
  96.     }
  97.  
  98.     /**
  99.      * Validates a New Zealand Regional Code
  100.      *
  101.      * @param       string     $region, regional code to validate
  102.      * @returns   bool
  103.      * @link        http://www.google.com/apis/adwords/developer/adwords_api_regions.html
  104.      */
  105.     function region($region)
  106.     {
  107.         $regions = array("AUK","BOP","CAN","GIS","HKB","MBH","MWT","NSN","NTL","OTA","STL","TAS","TKI","WGN","WKO","WTC");
  108.         return in_array(strtoupper($region),$regions);
  109.     }
  110.  
  111.     /**
  112.      * Validates a New Zealand phone number
  113.      *
  114.      * This function validates all New Zealand phone numbers
  115.      * checks for landline,0800,0900,0508,021,027 and 025 numbers
  116.      * allows for various combinations with spaces dashes and parentheses.
  117.      *
  118.      * @param string     $number, the number to validate
  119.      * @returns bool
  120.      * @link
  121.      */
  122.     function phoneNumber($number$requireAreaCode = true)
  123.     {
  124.         
  125.        $number str_replace(array("-"," ","(",")","+"),'',trim($number));
  126.        
  127.        // Is landline with country and area codes.
  128.        if (preg_match("(^640(3|4|6|7|9)[0-9]{7})",$number)) {
  129.            return true;
  130.        }
  131.        
  132.         // Is land line with area code
  133.         if ($requireAreaCode && preg_match("(^0(3|4|6|7|9)[0-9]{7}$)",$number)) {
  134.             return true;
  135.         }
  136.         
  137.         // Is Landline without area code.
  138.         if (!$requireAreaCode && preg_match("(^[0-9]{7}$)",$number)) {
  139.             return true;
  140.         }
  141.         
  142.         // Is Mobile number (021,025 or 027)
  143.         if (preg_match("(^02(1|5|7)[0-9]{3}[0-9]{4}$)",$number)) {
  144.             return true;
  145.         }
  146.  
  147.         // Is either a 0800,0900 or 0508 number
  148.         if (preg_match("(^0(8|9|5)0(0|8)[0-9]{6}$)",$number)) {
  149.             return true;
  150.         }
  151.  
  152.         return false;
  153.     }
  154.  
  155.  
  156.     /**
  157.      * Validates a New Zealand Bank Account Number
  158.      *
  159.      * This function checks wheather the given value
  160.      * is a valid New Zealand bank account number.
  161.      * allows several formats including;
  162.      *
  163.      *   xx-xxxx-xxxxxxx-xx
  164.      *   xx xxxx xxxxxxx xx
  165.      *   xxxxxxxxxxxxxxx
  166.      *
  167.      * @param string     $Value number to validate
  168.      * @returns bool
  169.      */
  170.     function bankCode($bankcode)
  171.     {
  172.         $bankcode str_replace(array("-"," "),'',trim($bankcode));
  173.         return preg_match("(^[0-9]{15}$)",$bankcode);
  174.     }
  175. }

Documentation generated on Mon, 11 Mar 2019 14:41:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.