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

Source for file IN.php

Documentation is available at IN.php

  1. <?php
  2.     
  3. /**
  4.  * Specific validation for data pertaining to the Republic of India.
  5.  *
  6.  * This class will validate Indian:
  7.  *  - Postal Codes (Zip Codes)
  8.  *  - State/U.T. Codes
  9.  *  - Telephone Numbers
  10.  *  - PAN/TAN Numbers
  11.  *  - Vehicle license plate Numbers
  12.  *
  13.  * @category   Validate
  14.  * @package    Validate_IN
  15.  * @author     Anant Narayanan <anant@php.net>
  16.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  17.  */
  18.  
  19. {
  20.     // {{{ pan()
  21.     
  22.     /**
  23.      * Validates an Indian Permanent Account Number (PAN) or
  24.      * Tax deduction and collection Account Number (TAN).
  25.      *
  26.      * @param   string  $number     The PAN or TAN to be validated.
  27.      *
  28.      * @return  boolean TRUE if code is valid, FALSE otherwise
  29.      * 
  30.      * @access  public
  31.      * @static
  32.      */
  33.     function pan($number)
  34.     {
  35.         return (bool)preg_match('/^[A-Z]{3}[A-Z0-9]{7}$/'$number);
  36.     }
  37.  
  38.     //}}}
  39.     //{{{ postalCode()
  40.     
  41.     /**
  42.      * Validates an Indian Postal Code (ZIP code)
  43.      *
  44.      * @param   string  $postalCode The ZIP code to validate
  45.      *
  46.      * @return  boolean TRUE if code is valid, FALSE otherwise
  47.      *
  48.      * @access  public
  49.      * @static
  50.      */
  51.     function postalCode($postalCode)
  52.     {
  53.         return (bool)preg_match('/^[1-9]{1}[0-9]{2}(\s|\-)?[0-9]{3}$/'$postalCode);
  54.     }
  55.  
  56.     //}}}
  57.     //{{{ stateCode()
  58.  
  59.     /**
  60.      * Validates a state / union territory code and returns the full name of the
  61.      * state / union territory code passed.
  62.      *
  63.      * @param   string  $region     2-letter state / union territory code
  64.      *
  65.      * @return  bool    True if state code is valid, False otherwise.
  66.      * 
  67.      * @access  public
  68.      * @static
  69.      */
  70.     function stateCode($stateCode)
  71.     {
  72.         switch (strtoupper($stateCode)) {
  73.             case 'AN':  
  74.             case 'AP'
  75.             case 'AR'
  76.             case 'AS'
  77.             case 'BR':  
  78.             case 'CH'
  79.             case 'CT':  
  80.             case 'DN'
  81.             case 'DD'
  82.             case 'DL':  
  83.             case 'GA':  
  84.             case 'GJ':  
  85.             case 'HR':  
  86.             case 'HP':  
  87.             case 'JK'
  88.             case 'JH':  
  89.             case 'KA':  
  90.             case 'KL':  
  91.             case 'LD':  
  92.             case 'MP'
  93.             case 'MH'
  94.             case 'MN'
  95.             case 'ML':  
  96.             case 'MZ':  
  97.             case 'NL':  
  98.             case 'OR'
  99.             case 'PY':  
  100.             case 'PB'
  101.             case 'RJ':  
  102.             case 'SK'
  103.             case 'TN'
  104.             case 'TR':  
  105.             case 'UL'
  106.             case 'UP':  
  107.             case 'WB':  
  108.                 return true;
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     //}}}
  114.     //{{{ getStateName()
  115.  
  116.     /**
  117.      * Returns the full name of a state / union territory given a valid state
  118.      * code. If state code is invalid or NULL, an array of all states is
  119.      * returned.
  120.      *
  121.      * @param   String  $code   2 letter State / U.T. Code.
  122.      *
  123.      * @return  String/Array    Full name of state of code is valid, array of
  124.      *                           all states if not.
  125.      *
  126.      * @access  public
  127.      * @static
  128.      */
  129.     function getStateName($code = NULL)
  130.     {
  131.         $states = array("Andaman and Nicobar Islands",
  132.                         "Andhra Pradesh",
  133.                         "Arunachal Pradesh",
  134.                         "Assam",
  135.                         "Bihar",
  136.                         "Chandigarh",
  137.                         "Chattisgarh",
  138.                         "Dadra and Nagar Haveli",
  139.                         "Daman and Diu",
  140.                         "Delhi",
  141.                         "Goa",
  142.                         "Gujarat",
  143.                         "Harayana",
  144.                         "Himachal Pradesh",
  145.                         "Jammu and Kashmir",
  146.                         "Jharkhand",
  147.                         "Karnataka",
  148.                         "Kerala",
  149.                         "Lakshwadeep",
  150.                         "Madhya Pradesh",
  151.                         "Maharashtra",
  152.                         "Manipur",
  153.                         "Meghalaya",
  154.                         "Mizoram",
  155.                         "Nagaland",
  156.                         "Orissa",
  157.                         "Pondicherry",
  158.                         "Punjab",
  159.                         "Rajasthan",
  160.                         "Sikkim",
  161.                         "Tamil Nadu",
  162.                         "Tripura",
  163.                         "Uttaranchal",
  164.                         "Uttar Pradesh",
  165.                         "West Bengal");
  166.  
  167.         switch (strtoupper($code)) {
  168.             case 'AN':  return $states[0];
  169.                         break;
  170.             case 'AP':  return $states[1];
  171.                         break;
  172.             case 'AR':  return $states[2];
  173.                         break;
  174.             case 'AS':  return $states[3];
  175.                         break;
  176.             case 'BR':  return $states[4];
  177.                         break;
  178.             case 'CH':  return $states[5];
  179.                         break;
  180.             case 'CT':  return $states[6];
  181.                         break;
  182.             case 'DN':  return $states[7];
  183.                         break;
  184.             case 'DD':  return $states[8];
  185.                         break;
  186.             case 'DL':  return $states[9];
  187.                         break;
  188.             case 'GA':  return $states[10];
  189.                         break;
  190.             case 'GJ':  return $states[11];
  191.                         break;
  192.             case 'HR':  return $states[12];
  193.                         break;
  194.             case 'HP':  return $states[13];
  195.                         break;
  196.             case 'JK':  return $states[14];
  197.                         break;
  198.             case 'JH':  return $states[15];
  199.                         break;
  200.             case 'KA':  return $states[16];
  201.                         break;
  202.             case 'KL':  return $states[17];
  203.                         break;
  204.             case 'LD':  return $states[18];
  205.                         break;
  206.             case 'MP':  return $states[19];
  207.                         break;
  208.             case 'MH':  return $states[20];
  209.                         break;
  210.             case 'MN':  return $states[21];
  211.                         break;
  212.             case 'ML':  return $states[22];
  213.                         break;
  214.             case 'MZ':  return $states[23];
  215.                         break;
  216.             case 'NL':  return $states[24];
  217.                         break;
  218.             case 'OR':  return $states[25];
  219.                         break;
  220.             case 'PY':  return $states[26];
  221.                         break;
  222.             case 'PB':  return $states[27];
  223.                         break;
  224.             case 'RJ':  return $states[28];
  225.                         break;
  226.             case 'SK':  return $states[29];
  227.                         break;
  228.             case 'TN':  return $states[30];
  229.                         break;
  230.             case 'TR':  return $states[31];
  231.                         break;
  232.             case 'UL':  return $states[32];
  233.                         break;
  234.             case 'UP':  return $states[33];
  235.                         break;
  236.             case 'WB':  return $states[34];
  237.                         break;
  238.             default:    return $states;
  239.                         break;
  240.         }
  241.     }
  242.  
  243.     //}}}
  244.     //{{{ phoneNumber()
  245.  
  246.     /**
  247.      * Validate an Indian Phone number.
  248.      *
  249.      * Allows the following formats:
  250.      *
  251.      *  (xxx) xxxxxxx
  252.      *  xxx xxxxxxx
  253.      *  +91 xxx xxxxxxx
  254.      *  091xxxxxxxxxx
  255.      *
  256.      * where whitespaces, dashes and brackets may interchanged freely and 0/+ may
  257.      * be added / skipped wherever possible.
  258.      *
  259.      * @param   string  $number         Phone number to validate (mobile or
  260.      *                                   landline)
  261.      *
  262.      * @return  bool    True if number is valid, False otherwise
  263.      *
  264.      * @access  public
  265.      * @static
  266.      */
  267.     function phoneNumber($number)
  268.     {
  269.         // strip country code
  270.         if (substr($number03)=='091' or substr($number03)=='+91'{
  271.             $number substr($number3strlen($number)-3);
  272.         }
  273.         // no numbers in India begin with 91, so safely strip country code
  274.         if (substr($number02)=='91'{
  275.             $number substr($number2strlen($number)-2);
  276.         }
  277.  
  278.         // it's a mobile number
  279.         if (preg_match('/^9(2|3|4|8|9)(\s)?(\-)?(\s)?[1-9]{1}[0-9]{7}$/'$number)) {
  280.             return true;
  281.         }
  282.  
  283.         // it's a landline, with or without area code
  284.         if (preg_match('/^\(?(0[1-9]{2,5}|\d{2,5})?(\s)?(\s|\)|\-)?(\s)?(2|3|5)\d{6,7}$/',
  285.             $number)) {          
  286.             return true;
  287.         }
  288.        
  289.         return false;
  290.     }
  291.  
  292.     //}}}
  293.     //{{{ licensePlate()
  294.  
  295.     /**
  296.      * Validates an Indian Vehicle's license plate number.
  297.      *
  298.      * @param   string  $license    The license plate number to validate.
  299.      *
  300.      * @return  boolean TRUE if code is valid, FALSE otherwise
  301.      *
  302.      * @access  public
  303.      * @static
  304.      */
  305.     function licensePlate($number)
  306.     {
  307.         if (Validate_IN::stateCode(substr($number02))) {
  308.             // state code is valid
  309.             return (bool)
  310.             preg_match('/^[A-Z]{2}(\s|\-)?[0-9]{1,2}(\s|\-)?(S|C|R|V)?(\s|\-)?[A-Z]{0,2}(\s|\-)?\d{4}$/',
  311.             $number);
  312.         }
  313.  
  314.         return false;
  315.     }
  316.     
  317.     //}}}
  318. }
  319.  
  320. /* END */
  321.  
  322. ?>

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