UK VAT Number Validator
A UK VAT number validator checks that a VAT registration number has the right shape and that its last two digits agree with the check-digit calculation HMRC uses, without contacting HMRC.
A UK VAT number is nine digits, usually written GB 123 4567 82, and the last two digits are computed from the first seven. HMRC has used two schemes: the original, where the weighted sum must divide by 97, and the 9755 scheme introduced when the first series ran low. This tool applies both, so it accepts every number HMRC has issued, and it also recognises the XI prefix used under the Northern Ireland protocol, twelve-digit VAT group branch numbers, and the GD and HA numbers used by government departments and health authorities. The generator produces numbers that pass the check, for use in forms, fixtures and validator tests.
Use cases
- Validating a VAT field before calling HMRC: Catch typos offline and only send well-formed numbers to the VAT number checking service, which is rate-limited and slow by comparison.
- Test data for invoicing and checkout: Generate VAT numbers that pass validation for B2B checkout tests, reverse-charge logic and invoice templates, without copying a real business’s number.
- Debugging a rejected number: See exactly why a number fails: wrong length, a prefix typo, or check digits that only pass under the scheme your validator does not implement.
Frequently asked questions
- Why does a VAT number pass here but fail in my own code?
Most hand-written validators only implement the original scheme. Numbers issued since HMRC introduced the 9755 scheme pass when the weighted sum plus 55 divides by 97, which this tool checks as well. Numbers below 100 0000 00 only use the original scheme.
- Does a valid result mean the business is VAT registered?
No. The check digits confirm the number is well formed. Only HMRC’s VAT number checking service can confirm registration, and it also returns the registered name and address.
- What is an XI VAT number?
The same registration quoted with an XI prefix instead of GB, for businesses trading goods under the Northern Ireland protocol. Validate the digits the same way.
How UK VAT numbers are checked
The two check-digit schemes, the prefixes, and what a pass does and does not prove · 600 words
1. Nine digits, two of them a check
A UK VAT registration number is nine digits, written in groups of three, four and two: 123 4567 82. The first seven are a serial number and the last two are calculated from them. Internationally it is quoted with a GB prefix, or XI for a business trading goods under the Northern Ireland protocol, and HMRC's own correspondence often shows the bare digits. A VAT group branch adds three more digits, making twelve. Government departments use GD followed by three digits from 000 to 499, and health authorities HA followed by 500 to 999.
A regular expression such as ^GB\d{9}$ accepts a billion strings; roughly one in fifty could ever be issued. The check digits are what separate them, and the calculation is public.
2. The calculation
Multiply the first seven digits by 8, 7, 6, 5, 4, 3 and 2 in turn and add the products. Add the two-digit check number. Under the original scheme the total must divide exactly by 97. For 123 4567 82: 8 + 14 + 18 + 20 + 20 + 18 + 14 = 112, and 112 + 82 = 194 = 2 × 97, so it passes.
When the original series approached exhaustion HMRC introduced the 9755 scheme, under which the weighted sum plus 55 must divide by 97. In practice that means the remainder after dividing by 97 is 42 instead of 0. The same seven digits with check number 27 give 139, and 139 mod 97 is 42, so 123 4567 27 is also valid. Numbers below 100 0000 00 only ever used the original scheme. A validator that knows one scheme and not the other rejects a large share of genuine numbers.
const weights = [8, 7, 6, 5, 4, 3, 2, 10, 1];
const digits = 'GB 123 4567 82'.replace(/\D/g, '');
const remainder = weights.reduce((sum, w, i) => sum + w * Number(digits[i]), 0) % 97;
const valid = Number(digits.slice(0, 3)) >= 100 ? remainder === 0 || remainder === 42 : remainder === 0;
3. Shape, registration, ownership
A passing check means the number is well formed. It does not mean the number is registered, still active, or belongs to the business quoting it. Those are three different questions. The first is answered offline by the code above, in microseconds, and belongs in the form. The second is answered only by HMRC's VAT number checking service, which returns the registered name and address for a live number. The third is a business decision made by comparing that name and address with the customer record.
The common mistakes are skipping the offline check and calling HMRC on every keystroke, or treating a passing checksum as proof of registration. Check the shape first, and only send numbers that pass it to the registry.
4. VAT numbers in test data
For automated tests you want numbers that pass the checksum, so they get past form validation, without being a real business's number. Generate rather than copy: pick seven digits at random and compute the check digits. Do not lift numbers from invoices or company websites into fixtures, because a real VAT number is tied to a real trading name and address, and test databases leak.
About one string in fifty passes, so a generated number can coincide 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 rather than relying on the number being unregistered.
More questions
- Should I strip the GB prefix before storing?
Store the nine or twelve digits and the prefix separately, and format on display. It makes matching and de-duplication reliable, and lets you show GB or XI as the context needs.
- Why does HMRC print the number with spaces?
For readability. Normalise spaces, dots and dashes away before validating rather than rejecting the field, because users paste the number exactly as it appears on an invoice.
- Do other EU countries use the same check?
No. Every member state has its own format and algorithm. The GB scheme described here applies only to UK numbers.