Solution To keep the implementation simple, this solution is split into two parts. First we strip out spaces and punctuation. Then we validate what remains. Strip whitespace and punctuation Retrieve the VAT number entered by the customer and store it into a variable. Before performing the check for a valid number, replace all matches of this regular expression with a blank replacement text: Any other extraneous characters will be caught by the upcoming check.
A single regular expression that matches VAT numbers from 27 countries in any possible notation is an impossible job. Since the punctuation is only for readability, it is much easier to first strip all the punctuation, then validate the resulting bare VAT number. Replacing all matches of this regular expression with nothing effectively deletes the punctuation characters commonly used in VAT numbers.
EU VAT number formats.