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

Source for file UK.php

Documentation is available at UK.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Specific validation methods for data used in the UK
  5.  *
  6.  * PHP Versions 4 and 5
  7.  *
  8.  * This source file is subject to the New BSD license, That is bundled
  9.  * with this package in the file LICENSE, and is available through
  10.  * the world-wide-web at
  11.  * http://www.opensource.org/licenses/bsd-license.php
  12.  * If you did not receive a copy of the new BSDlicense and are unable
  13.  * to obtain it through the world-wide-web, please send a note to
  14.  * pajoye@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category  Validate
  17.  * @package   Validate_UK
  18.  * @author    Michael Dransfield <mikeNO@SPAMblueroot.net>
  19.  * @author    Ian P. Christian <pookey@pookey.co.uk>
  20.  * @author    Tomas V.V.Cox <cox@idecnet.com>
  21.  * @author    Pierre-Alain Joye <pajoye@php.net>
  22.  * @copyright 1997-2005 Michael Dransfield, Ian P. Christian, Pierre-Alain Joye
  23.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  24.  * @version   CVS: $Id: UK.php 243194 2007-09-29 23:23:34Z kguest $
  25.  * @link      http://pear.php.net/package/Validate_UK
  26.  */
  27.  
  28. /**
  29.  * Data validation class for the UK
  30.  *
  31.  * This class provides methods to validate:
  32.  *  - SSN (National Insurance/NI Number)
  33.  *  - Sort code
  34.  *  - Bank account number
  35.  *  - Postal code
  36.  *  - Telephone number
  37.  *  - Car registration number
  38.  *  - Passport
  39.  *  - Driving license
  40.  *
  41.  * @category  Validate
  42.  * @package   Validate_UK
  43.  * @author    Michael Dransfield <mikeNO@SPAMblueroot.net>
  44.  * @author    Ian P. Christian <pookey@pookey.co.uk>
  45.  * @copyright 1997-2005 Michael Dransfield, Ian P. Christian
  46.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  47.  * @version   Release: @package_version@
  48.  * @link      http://pear.php.net/package/Validate_UK
  49.  */
  50. {
  51.     /**
  52.      * validates a postcode
  53.      *
  54.      * Validation according to the "UK Government Data Standards Catalogue"
  55.      * Using PostCode-format version 2.1, which can be obtained from:
  56.      * http://www.govtalk.gov.uk/gdsc/html/noframes/PostCode-2-1-Release.htm
  57.      *
  58.      * Note: The official validation-pattern was altered to also support postcodes
  59.      * with none or even spaces at various places. We don't count spaces as being
  60.      * "essential" for the validation-process.
  61.      * It was also necessary to refactor the regex to make it usable for preg_match.
  62.      *
  63.      * @param string $postcode the postcode to be validated
  64.      * @param bool   $strong   optional; strong checks (e.g. against a list
  65.      *                          of postcodes) (not implanted)
  66.      *
  67.      * @access    public
  68.      * @return    bool 
  69.      */
  70.     function postalCode($postcode$strong = false)
  71.     {
  72.         // $strong is not used here at the moment; added for API compatibility
  73.         // checks might be added at a later stage
  74.  
  75.         // remove spaces and uppercase it
  76.         $postcode strtoupper(str_replace(' '''$postcode));
  77.         $preg     "/^([A-PR-UWYZ]([0-9]([0-9]|[A-HJKSTUW])?|[A-HK-Y][0-9]"
  78.                   . "([0-9]|[ABEHMNPRVWXY])?)[0-9][ABD-HJLNP-UW-Z]{2}|GIR0AA)$/";
  79.         $match    preg_match($preg$postcode? true : false;
  80.         return $match;
  81.     }
  82.  
  83.     /**
  84.      * Validates a social security number whic in UK is
  85.      * National Insurance Number or ni for short
  86.      *
  87.      * Validation according to the "UK Government Data Standards Catalogue"
  88.      * Using NationalInsuranceNumber-format version 2.1, which can be obtained from:
  89.      * www.govtalk.gov.uk/gdsc/html/noframes/NationalInsuranceNumber-2-1-Release.htm
  90.      *
  91.      * Note: The official validation-pattern was altered to also support numbers
  92.      * with none or even spaces at various places. We don't count spaces as being
  93.      * "essential"
  94.      * for the validation-process.
  95.      *
  96.      * @param string $ssn NI number
  97.      *
  98.      * @access public
  99.      * @return bool 
  100.      */
  101.     function ssn($ssn)
  102.     {
  103.         // remove spaces and uppercase it
  104.         $ssn  strtoupper(str_replace(' '''$ssn));
  105.         $preg "/^[A-CEGHJ-NOPR-TW-Z][A-CEGHJ-NPR-TW-Z][0-9]{6}[ABCD]?$/";
  106.         if (preg_match($preg$ssn)) {
  107.             $bad_prefixes = array('GB''BG''NK''KN''TN''NT''ZZ');
  108.             return (array_search(substr($ssn02)$bad_prefixes=== false);
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     /**
  114.      * Validates a sort code, must be passed with dashes in the right places
  115.      *
  116.      * @param string $sc the sort code
  117.      *
  118.      * @access public
  119.      * @return bool 
  120.      * @see
  121.      */
  122.     function sortCode($sc)
  123.     {
  124.         // must be in format nn-nn-nn (must contain dashes)
  125.         // need to research the range of values - i have assumed 00-00-00 to 99-99-99
  126.         // but it might be something like 01-01-01 to 50-99-99
  127.         $preg  "/^[0-9]{2}\-[0-9]{2}\-[0-9]{2}$/";
  128.         $match (preg_match($preg$sc)) ? true : false;
  129.         return $match;
  130.     }
  131.  
  132.     /**
  133.      * Validates a bank ac number
  134.      *
  135.      * do not use - it is too basic at the moment
  136.      *
  137.      * @param string $ac the account number
  138.      *
  139.      * @access public
  140.      * @return bool 
  141.      */
  142.     function bankAC($ac)
  143.     {
  144.         // just checking to see if it is 6-8 digits
  145.         // FIXME *THIS IS PROBABLY WRONG!!! RESEARCH*
  146.         // There is a modulus 10/11 system that could be implemented here, but
  147.         // it's potentially quite complex
  148.         // http://en.wikipedia.org/wiki/Luhn_formula - Ian
  149.         $preg  "/^[0-9]{6,8}$/";
  150.         $match (preg_match($preg$ac)) ? true : false;
  151.         return $match;
  152.     }
  153.  
  154.     /**
  155.      * Checks that the entry is a number starting with 0 of the right length
  156.      *
  157.      * @param string $number the tel number
  158.      *
  159.      * @access public
  160.      * @return bool 
  161.      * @see
  162.      */
  163.     function phoneNumber($number)
  164.     {
  165.         //phone number can't include letters.
  166.         if (preg_match("/[A-Z]/i"$number!= 0{
  167.             return false;
  168.         }
  169.         $number preg_replace('/\D+/'''$number);
  170.         $len    strlen($number);
  171.         return $number[0== 0 // first number is 0
  172.             && (($len == 11 && ($number[1!= "0"))    // 11 digits is fine
  173.             || ($len == 10     // 10 digits is fine if 01 or 08
  174.                 && ($number[1== 1 || $number[1== 8)));
  175.     }
  176.  
  177.     /**
  178.      * Validates a car registration number
  179.      *
  180.      * @param string $reg the registration number
  181.      *
  182.      * @access public
  183.      * @return bool 
  184.      */
  185.     function carReg($reg)
  186.     {
  187.         include_once 'Validate/UK/carReg.php';
  188.         $reg strtoupper(str_replace(array('-'' ')''$reg));
  189.         // functions to check, in order
  190.         $regFuncs = array(
  191.             '2001',
  192.             '1982',
  193.             '1963',
  194.             '1950',
  195.             '1932',
  196.             'Pre1932'
  197.         );
  198.         foreach ($regFuncs as $func{
  199.             $cfunc 'validateVehicle' $func;
  200.             $ret   $cfunc($reg);
  201.             if ($ret !== false{
  202.                 // maybe return something useful here when possible?
  203.                 return true;
  204.             }
  205.         }
  206.         return false;
  207.     }
  208.  
  209.     /**
  210.      * Validates a UK passport number.
  211.      *
  212.      * EU might be the same just checks for 9 digits
  213.      *
  214.      * @param string $pp the passport number
  215.      *
  216.      * @access public
  217.      * @return string 
  218.      */
  219.     function passport($pp)
  220.     {
  221.         // just checks for 9 digit number
  222.         return (ctype_digit($pp&& strlen($pp== 9);
  223.     }
  224.  
  225.     /**
  226.      * Validates a UK driving licence
  227.      *
  228.      * @param string $dl the driving license
  229.      *
  230.      * @access public
  231.      * @return bool 
  232.      */
  233.     function drive($dl)
  234.     {
  235.         $dl    strtoupper(str_replace(' '''$dl));
  236.         $preg  "/^[A-Z]{5}[0-9]{6}[A-Z0-9]{5}$/";
  237.         $match (preg_match($preg$dl)) ? true : false;
  238.         return $match;
  239.     }
  240. }
  241. ?>

Documentation generated on Sun, 10 Oct 2010 14:30:04 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.