Skip to content

Instantly share code, notes, and snippets.

@passatgt
Created April 7, 2025 19:37
Show Gist options
  • Save passatgt/bee5e3685293967c11e04191db5eee21 to your computer and use it in GitHub Desktop.
Save passatgt/bee5e3685293967c11e04191db5eee21 to your computer and use it in GitHub Desktop.
Regex to validate EU vat numbers
<?php
$vat_number = 'HU12345678';
if(!preg_match('/^((AT)?U[0-9]{8}|(BE)?[0-9]{10}|(BG)?[0-9]{9,10}|(CY)?[0-9]{8}[A-Z]|(CZ)?[0-9]{8,10}|(DE)?[0-9]{9}|(DK)?[0-9]{8}|(EE)?[0-9]{9}|(EL|GR)?[0-9]{9}|(ES)?[0-9A-Z][0-9]{7}[0-9A-Z]|(FI)?[0-9]{8}|(FR)?[0-9A-Z]{2}[0-9]{9}|(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3})|(HU)?[0-9]{8}|(IE)?[0-9]S[0-9]{5}[A-Z]|(IT)?[0-9]{11}|(LT)?([0-9]{9}|[0-9]{12})|(LU)?[0-9]{8}|(LV)?[0-9]{11}|(MT)?[0-9]{8}|(NL)?[0-9]{9}B[0-9]{2}|(PL)?[0-9]{10}|(PT)?[0-9]{9}|(RO)?[0-9]{2,10}|(SE)?[0-9]{12}|(SI)?[0-9]{8}|(SK)?[0-9]{10})$/', $vat_number)) {
return array(
"valid" => false
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment