# GSTIN Validator Code Generator

> Source: https://netray.co/tools/gstin-validator-code-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Get a Python function that checks GSTIN structure, state code and the mod 36 checksum character before you save a party.

## Category

developer

## Region

India

## Audience

- ERP developer
- Accountant
- Data engineer

## Tool

**Kind:** template

**Output:** code

**Language:** python

**Fields:**

#### Function name

**Key:** fn

**Type:** text

**Default:** is_valid_gstin

#### Sample GSTIN to test

**Key:** sample

**Type:** text

**Default:** 27AAPFU0939F1ZV

**Blocks:**

#### Python function

**Body:** import re

GSTIN_RE = re.compile(r"^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z][1-9A-Z]Z[0-9A-Z]$")
CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def gstin_checksum(first14):
    total = 0
    for i, ch in enumerate(first14):
        value = CHARS.index(ch) * (1 if i % 2 == 0 else 2)
        total += value // 36 + value % 36
    return CHARS[(36 - total % 36) % 36]

def {{fn}}(gstin):
    g = gstin.strip().upper()
    if not GSTIN_RE.match(g):
        return False
    return gstin_checksum(g[:14]) == g[14]

print({{fn}}("{{sample}}"))

#### What each part of a GSTIN means

**Table:**

**Columns:**

- Position
- Meaning
- Example

**Rows:**

**0:** 1-2

**1:** State code

**2:** 27

**0:** 3-12

**1:** PAN of the taxpayer

**2:** AAPFU0939F

**0:** 13

**1:** Registration number for the same PAN in the state

**2:** 1

**0:** 14

**1:** Default letter Z

**2:** Z

**0:** 15

**1:** Checksum character

**2:** V

**Notes:** A passing check only proves the format and checksum are consistent. It does not prove the GSTIN is active. Confirm status through the GST portal search or an authorised API before relying on it.

## Content

**Intro:** Bad GSTINs on customer and vendor masters cause rejected e-invoices and ITC problems later. This generator gives you a small Python function that checks the pattern and the checksum, so your import scripts and ERP integrations can reject typos early.

**Sections:**

#### How to use it

**Body:** Set the function name, paste the sample GSTIN you want to test, and copy the code into your data load script. Run it over the party master export from Tally, Busy, Marg or Zoho Books and review the rows that return False. Fix those at source before migration or e-invoice onboarding.

#### How the checksum works

**Body:** Each of the first 14 characters is mapped to a value from 0 to 35, with digits first and then letters A to Z. Characters in even positions are multiplied by 1 and odd ones by 2. For each product the quotient and remainder after dividing by 36 are added up. The check character is the value of 36 minus the total modulo 36, taken modulo 36 again.

**Bullets:**

- The regex checks length, state digits, PAN pattern and the letter Z.
- The checksum catches most single character typos.
- Neither check shows whether the registration is active.

#### Where to use it in an ERP

**Body:** Run it as a pre-save validation in a custom script or integration layer. SAP Business One India, Zoho Books and Tally each have their own built-in GSTIN checks of varying depth, so this is most useful in bulk loads and in systems without them. For active status use the official portal service.

#### Common mistakes

**Body:** Entering a lowercase letter, trailing spaces from Excel, or confusing the letter O with zero are the usual causes of failures. The function trims and uppercases input, but letter and digit confusion needs a human fix.

**Cta:** We can help you add data quality checks like this on top of your ERP master data, and keep them read-only until an accountant approves a fix.

**Faq:**

#### What is the GSTIN format?

**Answer:** A GSTIN has 15 characters: 2 digit state code, the 10 character PAN, an entity number for that PAN in the state, the letter Z, and a checksum character.

#### Does a valid checksum mean the GSTIN is active?

**Answer:** No. It only proves the number is internally consistent. A cancelled or fake registration can still pass, so use the GST portal search for status.

#### Can I use the regex in Excel?

**Answer:** Newer Excel versions support regular expression functions, and older ones need VBA or Power Query. Python is simpler for bulk checks across thousands of rows.

#### Why does a real GSTIN fail?

**Answer:** Check for spaces, lowercase letters, the letter O versus zero, or a copy error. If none apply, compare the number with the registration certificate.

## Search terms

gstin validation python, gstin regex, gstin checksum algorithm, validate gst number, gstin format check, gst number validation code, gstin check digit, gstin regex excel

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
