Sunday, May 31, 2015

Validate Indian PAN Using Go Regular Expression

PAN (Permanent Account Number) is a unique 10 character alpha-numeric code issued by Indian Income Tax Department. It's a credit card size card issued with the basic details of the entity (individual, company, trust, firm etc.) and the 10 character code printed on it. A PAN is mandatory for majority of financial transactions.

Note: The code in this article will do only a basic validation by pattern matching to rule out the junk entries. It CAN NOT be used to verify if the PAN code entered is genuine.

Do You Want to Test the Code in Action in Your Browser?
  • Before going through the entire article, would you like to check this code in action? Visit the page, click run and follow the prompt.
PAN Card Format and Rules
  1. 10 character alpha-numeric.
  2. AAAPL1234C: First five characters are letters, next four numerals, last character letter.
  3. The 1st 3 letters are sequnce of alphabets from AAA to ZZZ
  4. The 4th character informs - Type of holder of card:
                         A — Association of Persons (AOP)
                         B — Body of Individuals (BOI)
                         C — Company
                         F — Firm
                         G — Government
                         H — HUF (Hindu Undivided Family)
                         L — Local Authority
                         J — Artificial Judicial Person
                         P — Individual
                         T — AOP (Trust)

    5. The 5th character is an Alphabet [A to Z]
    6. The 10th or last character is an alphabet.

Validate Using Regular Expression (Regex)

Regex is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. To understand the code, beginners must pay attention to the comment blocks in below code snippet.

package main

import (

func main() {
    For details

 fmt.Println("Enter PAN code")
 var pan string
 fmt.Scanf("%s", &pan)
 //The string entered is converted to upper case
 pan = strings.ToUpper(pan)
 fmt.Println("You entered: ", pan)
    Regular Expression
    1. import regexp package
    2. ^ indicates "at the beginning of string"
    3. $ indicates "at the end of string"
    4. [A-Z] indicates any upper case alphabet from A to Z both inclusive
    5. {3} exactly 3
    6. [0-9] indiates any numeric between 0 & 9 both inclusive

 re := regexp.MustCompile("^[A-Z]{3}[ABCFGHLJPT]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}$")

 var t, msg string

 //If the pattern doesn't match MatchString will return "false"

 if !re.MatchString(pan) {
  msg = "PAN format is invalid"

 } else {

  //It's a valid PAN, Let us check the 4th character to find Type of Holder
  panType := pan[3:4]

  switch panType {
  case "A":
      t = "Association of Persons (AOP)"
  case "B":
      t = "Body of Individuals (BOI)"
  case "C":
      t = "Company"
  case "F":
      t = "Firm"
  case "G":
      t = "Government"
  case "H":
      t = "HUF (Hindu Undivided Family)"
  case "L":
      t = "Local Authority"
  case "J":
      t = "Artificial Judicial Person"
  case "P":
      t = "Individual"
  case "T":
      t = "AOP (Trust)"
  fmt.Println("PAN is valid; type of holder of PAN card: ", t)

What Next?

Play with the code here

Learn more about RegExp syntax

We can write a function that takes PAN as input parameter and returns the valid or invalid message to the calling function.

Is the code written above idiomatic Go? I'm learning, appreciate if you can share your feedback to improve the code. Spread the word if you like this :)

No comments:

Post a Comment