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.      * @access   public
  39.      * @param    string    $postcode, postcode to validate
  40.      * @param    bool      $strong, optional; strong checks against a list of postcodes
  41.      * @return   bool      The valid or invalid postal code
  42.      * @link     http://www.nzpost.co.nz/nzpost/images/addressing.nzpost/pdfs/postcodedirectory_nomaps.pdf
  43.      */
  44.     function postalCode($postcode,$strong = false)
  45.     {    
  46.         if (!ctype_digit($postcode|| $postcode > 0110 && $postcode < 9822{
  47.             return false;
  48.         else {
  49.         
  50.             if ($strong {
  51.                 
  52.                 $postcodes = array("0110","0420","0310","1010","0610","0600","2012","2105",
  53.                                    "0505","1081","1022","2102","2010","2022","2013","0630",
  54.                                    "0614","0612","2014","1025","0931","3210","3214","3204",
  55.                                    "3200","3410","3118","3112","3015","4130","4122","4110",
  56.                                    "4312","4501","4500","4310","4825","4820","5032","5024",
  57.                                    "5510","4410","5036","5018","6022","5010","6011","6037",
  58.                                    "5028","5034","7281","7173","7196","7073","7183","7005",
  59.                                    "7007","7022","7025","7071","7072","7077","7081","7091",
  60.                                    "7095","7096","7220","7110","7120","7282","7284","7175",
  61.                                    "7182","7194","7192","7193","7195","7197","7198","7204",
  62.                                    "7210","7100","7271","7272","7273","7274","7275","7276",
  63.                                    "7285","7178","7201","7010","7011","7020","8013","8041",
  64.                                    "7334","8011","7402","8022","8083","8062","7999","8024",
  65.                                    "8053","8051","7832","7300","7802","8042","8025","8081",
  66.                                    "7610","7825","8014","9810","9016","9010","9822","9400",
  67.                                    "9012","9014","9022","9013","9023","9401","9812");
  68.  
  69.                 return in_array($postcode$postcodes);
  70.             }
  71.         }
  72.         return preg_match('^[0-9]{4}$'$postcode);
  73.     }
  74.  
  75.     /**
  76.      * Validates a New Zealand IRD Number (ssn equiv)
  77.      *
  78.      * recently the format has changed to having a
  79.      * prefix of 0, this will work with both new and old IRD numbers.
  80.      *
  81.      * @access  public
  82.      * @param   string     $ssn,  IRD number to validate
  83.      * @return  bool       The valid or invalid ird number
  84.      */
  85.     function ssn($ssn)
  86.     {
  87.         $ssn str_replace(array("-"," ","."),'',trim($ssn));
  88.  
  89.         if (!ctype_digit($ssn)) {
  90.             return false;
  91.         }
  92.         
  93.         switch ($ssn{
  94.             case 8:
  95.                 return true;
  96.             break;
  97.             case 9:
  98.                 if ($ssn{0== "0"{
  99.                     return true;
  100.                 }
  101.             break;
  102.         }
  103.         
  104.         return false;
  105.         
  106.     }
  107.  
  108.     /**
  109.      * Validates a New Zealand Regional Code
  110.      *
  111.      * @access    public
  112.      * @param     string     $region, regional code to validate
  113.      * @return    bool       The valid or invalid regional code
  114.      * @link      http://www.google.com/apis/adwords/developer/adwords_api_regions.html
  115.      */
  116.     function region($region)
  117.     {
  118.        $regions = array("AUK","BOP","CAN","GIS","HKB","MBH","MWT","NSN","NTL","OTA","STL","TAS","TKI","WGN","WKO","WTC");
  119.        return in_array(strtoupper($region),$regions);
  120.     }
  121.     
  122.     /**
  123.      * Validates a New Zealand phone number
  124.      *
  125.      * This function validates all New Zealand phone numbers
  126.      * checks for landline,0800,0900,0508,021,027 and 025 numbers
  127.      * allows for various combinations with spaces dashes and parentheses.
  128.      *
  129.      * @access    public
  130.      * @param     string     $number, the number to validate
  131.      * @param     bool       $requireAreaCode, require the area code? (default: true)
  132.      * @return    bool       The valid or invalid phone number
  133.      */
  134.     function phoneNumber($number$requireAreaCode = true)
  135.     {
  136.         
  137.        $number str_replace(array("+"," ","(",")","-"),array("00","","","",""),trim($number));
  138.        
  139.        if (!ctype_digit($number)) {
  140.            return false;
  141.        else {
  142.            
  143.            $numlength strlen($number);
  144.            
  145.            switch ($numlength{
  146.                   case 7:
  147.                       if (!$requireAreaCode{
  148.                           $regexp "(^[0-9]{7}$)"// Is land line w/o area code
  149.                       }
  150.                   break;
  151.                case 9:
  152.                       $regexp "(^0(3|4|6|7|9)[0-9]{7}$)";   // Is land line with area code
  153.                   break; 
  154.                   case 10:
  155.                       if (in_array(substr($number,0,4),array("0800","0900","0508"))) 
  156.                           $regexp "(^0(8|9|5)0(0|8)[0-9]{6}$)"// Is 0800,0900 or 0508 number
  157.                       elseif (in_array(substr($number,0,3),array("021","025","027"))) {
  158.                           $regexp "(^02(1|5|7)[0-9]{3}[0-9]{4}$)"//Is Mobile number
  159.                       }
  160.                   break;
  161.                   case 11:
  162.                       if (substr($number,0,3== "640"{
  163.                           $regexp "(^640(3|4|6|7|9)[0-9]{7})";   // Is land line with country code
  164.                       }
  165.                   break;
  166.                   case 13:
  167.                       if (substr($number,0,4== "0064"{
  168.                           $regexp "(^00640(3|4|6|7|9)[0-9]{7})";     // Is land line with country code and 00
  169.                       }
  170.                   break;
  171.               }
  172.        }
  173.        
  174.        if ($regexp{
  175.            return preg_match($regexp,$number);
  176.        }
  177.  
  178.         return false;
  179.     }
  180.  
  181.  
  182.     /**
  183.      * Validates a New Zealand Bank Account Number
  184.      *
  185.      * This function checks wheather the given value
  186.      * is a valid New Zealand bank account number.
  187.      * allows several formats.
  188.      *
  189.      * @access    public
  190.      * @param     string     $bankcode, number to validate
  191.      * @return    bool       The valid or invalid Bank Account Number
  192.      */
  193.     function bankCode($bankcode)
  194.     {
  195.         $bankcode str_replace(array("-"," ","."),'',trim($bankcode));
  196.         
  197.         if (ctype_digit($bankcode&& strlen($bankcode== 15{
  198.             return true;
  199.         }
  200.         
  201.         return false;
  202.     }
  203.     
  204.     /**
  205.      * Validates a New Zealand Vehicle license plates
  206.      *
  207.      * This function checks wheather the given value
  208.      * is a valid New Zealand Vehicle license plate
  209.      * 
  210.      * 6 characters for cars or trucks and
  211.      * 5 characters for motorbikes and trailors
  212.      *
  213.      * @access    public
  214.      * @param     string     $reg, string to validate
  215.      * @return    bool       The valid or invalid license plate number
  216.      */
  217.     function carReg($reg)
  218.     {
  219.         $reg trim($reg);
  220.         return (ctype_alnum($reg&& in_array(strlen($reg),array("6","5")));  
  221.     }
  222. }

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