Spain (ES)

Spanish VAT Number Validation

Format, pattern, and checksum algorithms for ES VAT numbers

Spanish VAT numbers are the most complex in the EU, with different formats and validation rules depending on whether the entity is a legal entity (CIF), Spanish citizen (DNI), or foreigner (NIE).

Format vs. Active Validation

This checks format and checksum only, not if a VAT number is really registered. Use VatDB's API to verify if a number is really registered and active. VatDB checks official EU and national datasources.

Format Overview

Property Value
Country Code ES
Format Letter/digit + 7 digits + letter/digit
Length 9 characters (11 characters with ES country prefix)
Examples ESA15075062 ESA28015865 ESA48265169

Regex Pattern

^ES[A-Z0-9][0-9]{7}[A-Z0-9]$

Types of Spanish VAT Numbers

CIF (Starts with A-H, J, N, P-W) - Legal Entities

CIF validation uses weighted sums with doubling. The same checksum value can be represented as a numeric control digit or an equivalent control letter:

def validate_cif(number):
    control_letters = "JABCDEFGHI"

    total = 0
    for i, digit in enumerate(number[1:8]):
        value = int(digit)
        if i % 2 == 0:
            value *= 2
            if value > 9:
                value = value // 10 + value % 10
        total += value

    control_digit = (10 - (total % 10)) % 10
    control_letter = control_letters[control_digit]
    first = number[0]
    check = number[8]

    if first in "KLM":
        return check == control_letter
    if first in "ABCDEFGHJNPQRSUVW":
        if check.isdigit():
            return int(check) == control_digit
        return check == control_letter
    return False
  • Double alternating digits (positions 1, 3, 5, 7 of the 7-digit body)
  • If a doubled value is > 9, sum its digits before adding
  • Control digit = (10 - (total % 10)) % 10, control letter = JABCDEFGHI[digit]
  • K, L, M: Always letter check
  • A-H, J, N, P-W: We recommend accepting either the numeric control digit or the equivalent control letter. Official documentation has conflicting information on which is required for each organization type.

DNI (Starts with digit) - Spanish Citizens

def validate_dni(number):
    letters = "TRWAGMYFPDXBNJZSQVHLCKE"
    digits = number[:8]
    check = number[8]
    return letters[int(digits) % 23] == check

NIE (Starts with X, Y, Z) - Foreign Residents

def validate_nie(number):
    letters = "TRWAGMYFPDXBNJZSQVHLCKE"
    prefix = {"X": "0", "Y": "1", "Z": "2"}
    digits = prefix[number[0]] + number[1:8]
    check = number[8]
    return letters[int(digits) % 23] == check

Important Notes

  • For CIF-style numbers, accept either the numeric control digit or equivalent control letter because official documentation conflicts on which is required.
  • Some Spanish VAT numbers are for domestic use only and won't appear in EU-wide systems. Use VatDB to check if a Spanish VAT number is active for intra-EU commerce.

Frequently Asked Questions

How do I validate a Spanish VAT number?

Spanish VAT validation depends on the format: CIF (starts with letter) uses weighted sums, DNI (starts with digit) uses MOD 23, and NIE (starts with X/Y/Z) converts the prefix then uses MOD 23. Use VatDB's API for official verification against EU and national databases.

What does a Spanish VAT number look like?

Spanish VAT numbers have format ES + 9 characters. Examples: ESA15075062, ESA28015865, ESA48265169. The format varies by entity type.

How many characters are in a Spanish VAT number?

A Spanish VAT number always has exactly 9 characters after the ES prefix: typically a letter/digit, 7 digits, and a letter/digit. The total length is 11 characters including ES.

What is the difference between Spanish NIF, CIF, and NIE?

NIF is the tax ID for Spanish citizens (based on DNI). NIE is for foreign residents. CIF was the old name for company tax IDs. All are now officially called NIF but the formats differ based on entity type.

Why is my Spanish VAT number not found when validating?

Some Spanish businesses are registered only for domestic VAT, not for intra-EU trade. VatDB's API checks against both EU and national databases for comprehensive verification.

How do I check if a Spanish company is VAT registered?

Verify VAT numbers using VatDB's API, which checks against official EU and national databases for comprehensive validation including domestic registrations.

What's the difference between format validation and checking if a VAT number is active?

Spain's format and checksum rules catch structural errors only. A number may pass the checksum tests, but be fake or inactive. VatDB's API queries official records for actual registration confirmation.

What are real world examples of Spanish VAT numbers?

Try these active, real Spanish (ES) VAT numbers: ESA15075062, ESA28015865, ESA48265169.

Validate Spanish VAT Numbers Instantly

Use our API for real-time ES VAT number validation across all formats

Try Free