The UK VAT Number Format Explained, with a Check-Digit Validator
Reference · November 2026 · 9 min read
What a UK VAT registration number looks like, how the two check-digit schemes work, a validator in JavaScript and Python, and how to use VAT numbers safely in test data.
A UK VAT registration number looks like nine digits after a GB prefix, and most forms validate it with nothing more than ^GB\d{9}$. That pattern accepts a billion strings, and only about one in fifty of them could ever be issued. The last two digits are a check, and the check is public.
What a UK VAT number looks like
The standard form is the country prefix followed by nine digits, usually written in groups of three, four and two:
GB 123 4567 82
└─┘ └──┘ └┘
serial check digits
The prefix is part of how the number is quoted internationally, not part of the number itself. HMRC's own correspondence often shows just the nine digits.
There are four shapes in total:
Since 2021, businesses trading goods under the Northern Ireland arrangements also quote the same number with an XI prefix instead of GB. It is the same registration, so validate the digits the same way and treat GB and XI as two labels for one number.
The check digits
The final two digits of a nine-digit number are calculated from the first seven. Multiply each of the first seven digits by a weight, counting down from 8 to 2:
Add the products, then add the two-digit check number. The result must be divisible by 97.
Working through 123 4567 82:
1×8 + 2×7 + 3×6 + 4×5 + 5×4 + 6×3 + 7×2
= 8 + 14 + 18 + 20 + 20 + 18 + 14
= 112
112 + 82 = 194 = 97 × 2 → passes
There is a second scheme. When the original series started to run out, HMRC introduced numbers whose check passes if you add 55 before dividing by 97. In practice that means the remainder is either 0 (original scheme) or 42 (the newer one, because 97 − 55 = 42). Using the same seven digits:
112 + 27 = 139, and 139 mod 97 = 42 → also passes
So 123 4567 82 and 123 4567 27 are both structurally valid, and a validator that only knows the first scheme will wrongly reject roughly half the numbers issued since the change. Numbers below 100 00 00 only ever used the original scheme.
A validator you can paste
This accepts the prefix, spaces, dots and dashes, and both check schemes:
function isValidUkVatNumber(input) {
const v = input.toUpperCase().replace(/[\s.-]/g, '').replace(/^(GB|XI)/, '');
if (/^GD\d{3}$/.test(v)) return Number(v.slice(2)) < 500;
if (/^HA\d{3}$/.test(v)) return Number(v.slice(2)) >= 500;
if (!/^\d{9}(\d{3})?$/.test(v)) return false;
// Weights 8..2 on the serial, then 10 and 1 fold the two check digits back in.
const weights = [8, 7, 6, 5, 4, 3, 2, 10, 1];
const remainder = weights.reduce((sum, w, i) => sum + w * Number(v[i]), 0) % 97;
return Number(v.slice(0, 3)) >= 100 ? remainder === 0 || remainder === 42 : remainder === 0;
}
isValidUkVatNumber('GB 123 4567 82'); // true (original scheme)
isValidUkVatNumber('GB123456727'); // true (newer scheme)
isValidUkVatNumber('GB123456789'); // false (check digits wrong)
isValidUkVatNumber('GBGD001'); // true
The weights 10, 1 on the last two positions are a shortcut: they turn the check digits back into a two-digit number and add it to the weighted sum in one pass.
The same logic in Python, for a back end or a data clean-up script:
import re
def is_valid_uk_vat_number(value: str) -> bool:
v = re.sub(r"[\s.-]", "", value.upper())
v = re.sub(r"^(GB|XI)", "", v)
if re.fullmatch(r"GD\d{3}", v):
return int(v[2:]) < 500
if re.fullmatch(r"HA\d{3}", v):
return int(v[2:]) >= 500
if not re.fullmatch(r"\d{9}(\d{3})?", v):
return False
weights = (8, 7, 6, 5, 4, 3, 2, 10, 1)
remainder = sum(w * int(d) for w, d in zip(weights, v)) % 97
return remainder in (0, 42) if int(v[:3]) >= 100 else remainder == 0
What the check does and does not tell you
A passing check means the number is well formed. It does not mean the number is registered, still active, or belongs to the business that typed it. Those are three separate questions:
1. Shape and checksum. Answered by the code above, offline, in microseconds. Use it to catch typos at the form. 2. Registration. Answered only by HMRC's own VAT number checking service, which returns the registered name and address for a live number. 3. Ownership. Whether the person entering it is entitled to use it. That is a business decision, usually made by comparing the registered name and address against the customer record.
The common mistake is skipping step 1 and calling the registry for every keystroke, or skipping step 2 and treating a passing checksum as proof. Run the offline check first, and only call the registry with numbers that pass it.
VAT numbers in test data
For automated tests you usually want numbers that pass the checksum so they get past form validation, without being a real business's number. Two practical rules:
- Generate, do not copy. Pick seven digits at random and compute the check digits with the formula above. Do not lift numbers from invoices or public company pages into fixtures. A real VAT number tied to a real trading name and address is business data, and test databases leak.
- Accept that a random valid number may exist. About one string in fifty passes, and HMRC has issued millions of numbers, so a generated number can collide with a live registration. That is harmless in a sandbox that never calls HMRC. If your test environment does call the live checking service, mock that call instead of relying on the number being unregistered. Mocking an external lookup is covered in mocking an address lookup API in tests.
Pair the number with a fictional trading address rather than a real one. The UK address generator produces structurally valid addresses on real postcode districts that are never live delivery points, and the company name generator gives you a trading name to go with it.
Common form bugs
- Rejecting the prefix. Users paste
GB123456782from an invoice. StripGBorXIbefore validating rather than failing the field. - Rejecting spaces. HMRC prints the number with spaces. Normalise before checking.
- Only implementing the original scheme. Covered above, and the most common real-world false rejection.
- Treating 12-digit numbers as invalid. VAT group members quote them. Validate the first nine and keep the branch suffix.
- Storing the formatted string. Store the nine (or twelve) digits and the prefix separately, and format on display. It makes matching and de-duplication reliable.
For the same "validate the structure, then verify with the authority" approach applied to bank details, see UK sort codes, account numbers and IBANs for test data.
More guides
- How to Check if a UK Address Is Real (and What “Valid” Actually Means)
- UK Bank Account Numbers: How Many Digits, How to Read One, and How to Check It
- The National Insurance Number Format Explained, with a Validator
- The NHS Number Format and Check Digit Explained
- UK Company Registration Numbers: Format and Prefixes Explained
- What Is a Post Town? How to Find Yours, and Why It Isn’t Always Your Town
- The UK Driving Licence Number Format Explained
- How Address Verification Software Works: PAF, UPRNs, Match Levels and What to Ask a Vendor
- Data Masking, Anonymisation and Pseudonymisation Under UK GDPR: What Each One Actually Means
- Validating UK Phone Numbers: Formats, Ofcom Ranges and the Mistakes Every Regex Makes
- Mocking a UK Address Lookup API in Your Tests
- Testing KYC and Identity Verification Flows Without Real Identities
- Test Data Management: Production Copies, Masked Data and Synthetic Data
- UK Sort Codes, Account Numbers, IBANs and SWIFT Codes for Test Data
- The UK Postcode Format Explained: Areas, Districts, Sectors and Units
- Royal Mail Address Formatting: How to Lay Out a UK Address Correctly
- How UK Address Generators Work — and How to Tell a Good One
- Using Real Customer Data in Test Environments: The UK GDPR Problem
- Why Your Address Form Rejects Perfectly Valid UK Addresses
- The UK Telephone Numbering Plan, and the Numbers Reserved for Fiction
- Validating UK Postcodes in JavaScript, Python and SQL
- Where UK Address Data Comes From: PAF, ONSPD, OS Open Names and AddressBase
- Why Faker's UK Postcodes Fail Validation