pincodescheckoutdevelopersuxtutorial

Pincode Validation and Address Autofill for Indian Checkouts

A wrong pincode is the cheapest error to prevent and one of the most expensive to ship. Here is how to validate pincodes and auto-fill city and state in your address form, with a short code example.

P
Postpin Team5 min read
Pincode Validation and Address Autofill for Indian Checkouts

Indian addresses are hard to standardise. Street names repeat, house numbering is inconsistent and landmarks do much of the work. The one reliable anchor is the six-digit PIN code: it identifies the delivery post office, and from it you can derive the district and state. A checkout that validates the pincode and fills in the rest prevents a surprising number of failed deliveries.

What goes wrong without validation

  • Typos. 560034 typed as 560043 is a valid-looking pincode in a different part of Bengaluru.
  • Mismatched state. The customer types 110001 and leaves the state dropdown on Maharashtra.
  • Old or made-up pincodes. Five digits, seven digits, or a pincode that no longer exists.
  • Spelling variants. Bangalore, Bengaluru, Banglore and B'lore are four values for the same city in your database.

Each of these can reach the courier as a parcel that cannot be routed. It then comes back as RTO, and you pay freight in both directions.

Level 1: format check

A PIN code is exactly six digits and never starts with zero. Check this in the browser before making any network call:

const isPincodeFormat = (v) => /^[1-9][0-9]{5}$/.test(v);

Also set the input up properly: inputmode="numeric", maxlength="6" and autocomplete="postal-code". On mobile this brings up the number pad and lets the browser fill a saved address.

A format check catches about half the junk. It cannot tell you that 999999 does not exist.

Level 2: existence and lookup

To know whether a pincode is real and where it is, you need the directory. The first digit of a PIN code is the postal region, the first two identify the circle or sub-region, the third the sorting district, and the last three the post office. That structure is useful for rough checks, but only the full directory tells you the city and state for certain. Our India Post pincode directory guide explains the structure.

A lookup call returns everything the address form needs:

GET /v1/serviceability/560034

{
  "pincode": "560034",
  "found": true,
  "city": "Bengaluru",
  "state": "Karnataka",
  "zone_tier": "metro",
  "is_remote": false,
  "sla_days": 3
}

Wiring it into the form

Call the lookup from your own backend so the API key is never exposed in the browser. A small Express route:

app.get("/api/pincode/:pin", async (req, res) => {
  const pin = req.params.pin;
  if (!/^[1-9][0-9]{5}$/.test(pin)) {
    return res.status(400).json({ found: false });
  }
  const r = await fetch(
    "https://api.postpin.in/v1/serviceability/" + pin,
    { headers: { authorization: "Bearer " + process.env.POSTPIN_KEY } }
  );
  res.json(await r.json());
});

And the browser side:

const pinInput = document.querySelector("#pincode");
const cityInput = document.querySelector("#city");
const stateInput = document.querySelector("#state");
const message = document.querySelector("#pincode-message");

let timer;
pinInput.addEventListener("input", () => {
  clearTimeout(timer);
  const pin = pinInput.value.trim();
  if (!/^[1-9][0-9]{5}$/.test(pin)) return;

  timer = setTimeout(async () => {
    const data = await fetch("/api/pincode/" + pin).then((r) => r.json());
    if (!data.found) {
      message.textContent = "We could not find this pincode. Please check it.";
      return;
    }
    cityInput.value = data.city;
    stateInput.value = data.state;
    message.textContent = "Delivers in about " + data.sla_days + " days";
  }, 300);
});

The 300 ms delay avoids firing a request on every keystroke. Since the call only goes out after six valid digits, most customers trigger exactly one.

Form design choices that matter

  • Ask for the pincode first. Put it above city and state, so those fields are filled before the customer reaches them.
  • Let the lookup own the state. State should be set from the pincode and shown read-only. It drives tax and zone decisions, so it should not be free text.
  • Keep city editable, with care. A pincode can cover several localities. Fill the city, but let the customer correct the locality or area in a separate field.
  • Make landmark a real field. Especially outside metros, a landmark does more for delivery success than a street name.
  • Handle "not found" gently. Show a clear message and let the customer re-enter. Log the pincode — repeated entries of the same unknown pincode point to demand you are not serving, or to a new pincode you should support.
  • Fail open. If the lookup times out, let the customer type city and state by hand. Validation must never block a sale because your own call failed.

Cache it

Pincode data changes, but not minute to minute. Cache each lookup on your server for a day, keyed on the pincode. Returning customers and popular pincodes then cost you no API calls at all. If you already use webhooks, clear the cache when the nightly directory sync completes.

What you get beyond a clean address

The same response also carries delivery information, so one call does three jobs:

  1. Validates the pincode and standardises city and state
  2. Confirms serviceability before the order is placed
  3. Sets the delivery promise, from the SLA for that pincode

From there, a rate call with the validated pincode gives the exact shipping charge. To see the data before integrating, try a few pincodes in the pincode lookup tool, then read the request and response fields in the API docs. The free plan includes 1,000 calls a month, and you can sign up without a card.

Frequently asked questions

How do I validate an Indian pincode?

First check the format: exactly six digits, not starting with zero. Then look it up against a current pincode directory to confirm it exists and to get its city and state. A format check alone accepts many pincodes that do not exist.

How can I auto-fill city and state from a pincode?

Call a pincode lookup API once the customer has typed six valid digits, and write the returned city and state into the form. Make the request through your own server so the API key stays private, and cache results to avoid repeat calls.

Can one pincode belong to more than one city or area?

Yes. A single pincode often covers several localities, and sometimes more than one post office. Auto-fill the main city and state, but keep a separate locality or area field the customer can edit.