How to validate a GSTIN in PHP using regex

In this tutorial, I will show you exactly how to validate a GSTIN in PHP using regex, with a checksum verification so you catch even subtly wrong numbers. I have also built a free live GSTIN validator tool at the bottom of this post — paste any GSTIN and it validates instantly.

Hi friends, actually i was wondering in internet to check the correct GSTIN but none of the proper checking tool found. So I thought it would be better to create a tool so that everyone can check the GSTIN validation before filling any invoice.

What is a GSTIN?

A GSTIN is a 15-character unique identification number assigned to every GST-registered business in India. It is printed on every tax invoice and is required for Input Tax Credit (ITC) claims.

The format is:

PositionCharactersMeaningExample
1–22 digitsState code (01–38)27 = Maharashtra
3–1210 charactersPAN number of taxpayerAAPFU0939F
131 digitEntity registration number (1–9)1
141 letterAlways the letter ZZ
151 alphanumericChecksum characterV

Full example: 27AAPFU0939F1ZV

Required steps to validate a GSTIN in PHP using regex

Method 1: Simple Regex Validation (Basic)

The quickest way to validate a GSTIN in PHP using Regex. This checks the structure but does not verify the checksum.

<?php

function validateGSTIN($gstin) {
    // Convert to uppercase
    $gstin = strtoupper(trim($gstin));

    // GSTIN regex pattern
    // 2 digits (state) + 5 letters + 4 digits + 1 letter + 1 alphanumeric + Z + 1 alphanumeric
    $pattern = '/^[0-3][0-9][A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]$/';

    if (strlen($gstin) !== 15) {
        return ['valid' => false, 'message' => 'GSTIN must be exactly 15 characters.'];
    }

    if (!preg_match($pattern, $gstin)) {
        return ['valid' => false, 'message' => 'GSTIN format is invalid.'];
    }

    return ['valid' => true, 'message' => 'GSTIN format is valid.'];
}

// Test it
$result = validateGSTIN('27AAPFU0939F1ZV');
echo $result['message']; // Output: GSTIN format is valid.

$result2 = validateGSTIN('INVALIDGSTIN123');
echo $result2['message']; // Output: GSTIN format is invalid.
?>

Method 2: Regex + Checksum Validation (Recommended)

The regex above only checks the format. A GSTIN can pass regex but still be wrong — for example if someone changes one character. The official GST checksum algorithm catches this. This is what the GST portal itself uses.

<?php

function validateGSTINWithChecksum($gstin) {
    $gstin = strtoupper(trim($gstin));

    // Step 1: Length check
    if (strlen($gstin) !== 15) {
        return ['valid' => false, 'message' => 'GSTIN must be exactly 15 characters.'];
    }

    // Step 2: Format check
    $pattern = '/^[0-3][0-9][A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]$/';
    if (!preg_match($pattern, $gstin)) {
        return ['valid' => false, 'message' => 'GSTIN format is invalid.'];
    }

    // Step 3: State code check (01 to 38)
    $stateCode = (int) substr($gstin, 0, 2);
    if ($stateCode < 1 || $stateCode > 38) {
        return ['valid' => false, 'message' => 'Invalid state code in GSTIN.'];
    }

    // Step 4: Checksum validation (official GST Mod-36 algorithm)
    $chars    = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $sum      = 0;

    for ($i = 0; $i < 14; $i++) {
        $charPos = strpos($chars, $gstin[$i]);
        if ($charPos === false) {
            return ['valid' => false, 'message' => 'Invalid character found in GSTIN.'];
        }
        $product  = $charPos * (($i % 2 === 0) ? 1 : 2);
        $sum     += (int)($product / 36) + ($product % 36);
    }

    $remainder     = $sum % 36;
    $expectedCheck = $chars[(36 - $remainder) % 36];

    if ($gstin[14] !== $expectedCheck) {
        return [
            'valid'   => false,
            'message' => 'GSTIN checksum is invalid. The number may have a typo.'
        ];
    }

    return ['valid' => true, 'message' => 'GSTIN is valid.'];
}

// ✅ Valid GSTIN
$result = validateGSTINWithChecksum('27AAPFU0939F1ZV');
var_dump($result);
// Output: ['valid' => true, 'message' => 'GSTIN is valid.']

// ❌ Wrong checksum (last character changed)
$result2 = validateGSTINWithChecksum('27AAPFU0939F1ZX');
var_dump($result2);
// Output: ['valid' => false, 'message' => 'GSTIN checksum is invalid...']
?>

Why use checksum validation? When a user types their GSTIN manually, they often swap one digit. The checksum catches these typos that the regex cannot. Always use Method 2 in production.

How to Use in a Form (With HTML)

Here is a complete working example with an HTML form and PHP validation together:

<?php
// gstin-form.php

function validateGSTINWithChecksum($gstin) {
    $gstin   = strtoupper(trim($gstin));
    $pattern = '/^[0-3][0-9][A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]$/';
    $chars   = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    if (strlen($gstin) !== 15)          return false;
    if (!preg_match($pattern, $gstin))  return false;
    if ((int)substr($gstin,0,2) < 1 || (int)substr($gstin,0,2) > 38) return false;

    $sum = 0;
    for ($i = 0; $i < 14; $i++) {
        $p    = strpos($chars, $gstin[$i]);
        $prod = $p * ($i % 2 === 0 ? 1 : 2);
        $sum += (int)($prod / 36) + ($prod % 36);
    }
    return $gstin[14] === $chars[(36 - ($sum % 36)) % 36];
}

$message = '';
$type    = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $gstin = $_POST['gstin'] ?? '';
    if (empty($gstin)) {
        $message = 'Please enter a GSTIN number.';
        $type    = 'error';
    } elseif (validateGSTINWithChecksum($gstin)) {
        $message = '✓ Valid GSTIN: ' . strtoupper(trim($gstin));
        $type    = 'success';
    } else {
        $message = '✗ Invalid GSTIN. Please check and try again.';
        $type    = 'error';
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>GSTIN Validator</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 500px; margin: 40px auto; padding: 0 20px; }
        input[type="text"] { width: 100%; padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 4px; }
        button { margin-top: 10px; padding: 10px 24px; background: #0066cc; color: white; border: none; border-radius: 4px; font-size: 15px; cursor: pointer; }
        .success { background: #d4edda; color: #155724; padding: 12px; border-radius: 4px; margin-top: 14px; }
        .error   { background: #f8d7da; color: #721c24; padding: 12px; border-radius: 4px; margin-top: 14px; }
    </style>
</head>
<body>
    <h2>GSTIN Validator</h2>
    <form method="POST">
        <label>Enter GSTIN Number:</label>
        <input type="text" name="gstin" maxlength="15" placeholder="e.g. 27AAPFU0939F1ZV"
               value="<?php echo htmlspecialchars($_POST['gstin'] ?? ''); ?>" />
        <button type="submit">Validate</button>
    </form>

    <?php if ($message): ?>
        <div class="<?php echo $type; ?>"><?php echo $message; ?></div>
    <?php endif; ?>
</body>
</html>

Laravel Version — Validation Rule

In Laravel, you can create a custom validation rule for GSTIN so you can reuse it anywhere in your application.

Step 1: Create the Rule

php artisan make:rule GstinRule

Step 2: Add the Logic

<?php
// app/Rules/GstinRule.php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class GstinRule implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $gstin   = strtoupper(trim($value));
        $pattern = '/^[0-3][0-9][A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]$/';
        $chars   = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        if (strlen($gstin) !== 15 || !preg_match($pattern, $gstin)) {
            $fail('The :attribute must be a valid 15-character GSTIN number.');
            return;
        }

        $stateCode = (int) substr($gstin, 0, 2);
        if ($stateCode < 1 || $stateCode > 38) {
            $fail('The :attribute contains an invalid state code.');
            return;
        }

        // Checksum
        $sum = 0;
        for ($i = 0; $i < 14; $i++) {
            $p    = strpos($chars, $gstin[$i]);
            $prod = $p * ($i % 2 === 0 ? 1 : 2);
            $sum += (int)($prod / 36) + ($prod % 36);
        }

        if ($gstin[14] !== $chars[(36 - ($sum % 36)) % 36]) {
            $fail('The :attribute checksum is invalid. Please check the GSTIN carefully.');
        }
    }
}

Step 3: Use in Controller or Request

<?php
// In your Controller or FormRequest

use App\Rules\GstinRule;

$request->validate([
    'gstin' => ['required', 'string', new GstinRule()],
]);

That’s it. Now Laravel will automatically validate the GSTIN and return a proper error message to your form.

Common Errors and Fixes

ErrorReasonFix
Fails on valid GSTINInput has lowercase lettersAlways run strtoupper() before validating
Fails on valid GSTINInput has spaces or hidden charactersAlways run trim() before validating
Checksum fails on real GSTINUser typed letter O instead of digit 0Show specific error “check character 7 or 8”
State code 00 passes regexPattern allows 00Add explicit check: $stateCode >= 1
Regex pattern not matchingWrong delimiter in PHPUse /pattern/ not #pattern# for clarity

Free Live GSTIN Validator Tool

I built a free online tool so you can validate any GSTIN instantly — no login, no signup. It runs the same regex + checksum logic shown above and shows you the full breakdown including state name, PAN, taxpayer type, and whether the checksum is correct.

👇 Try it right here:

Conclusion:-

To summarise what we covered:

  • A GSTIN is 15 characters — 2-digit state code + 10-char PAN + entity number + Z + checksum
  • Use Method 1 (regex only) for quick format checks in JavaScript on the frontend
  • Use Method 2 (regex + checksum) in PHP backend before saving to database — always
  • In Laravel, create a custom GstinRule so you can reuse it across all your forms
  • Always strtoupper() and trim() the input before validating

If you found this useful, check out my other India-specific developer tools and tutorials:

If you have any questions or face any errors with the code, drop a comment below. I usually reply within a day.

Leave a Comment

thor311

thor311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

HOKI311

HOKI311

sbobet WAP

hoki311

sabung ayam online

hoki311

hoki311

live casino online

HOKI311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

mix parlay sbobet

hoki311

hoki311

hoki 311

togel hoki311

hoki311

THOR311

THOR311

THOR311

ceri188

ceri188

ceri188

ratu311 slot

THOR311

kupu178 toto hongkong

hoki311

hoki311

SABA Sport

sbobet

hoki311

THOR311

RATU311

kupu178 togel online

kupu178 slot demo

slot ratu311

pola slot gacor

ratu311

live casino online

hoki311

slot gacor online

hoki311

mix parlay

hoki311

sabung ayam online

hoki311

THOR311

THOR311

HOKI311

SBOBET WAP

Judi Bola Online

hoki311

hoki311

slot gacor

sabung ayam online

hoki311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

ceri188

togel online

hoki311

hoki311

sabung ayam online

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311