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

Documentation generated on Tue, 27 Mar 2007 17:30:03 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.