4.21. European VAT Numbers
Problem
You’re given the job of implementing an online order form for a business in the European Union.
European tax laws stipulate that when a VAT-registered business (your customer) located in one EU country purchases from a vendor (your company) in another EU country, the vendor must not charge VAT (Value-Added Tax). If the buyer is not VAT-registered, the vendor must charge VAT and remit the VAT to the local tax office. The vendor must use the VAT registration number of the buyer as proof to the tax office that no VAT is due. This means that for the vendor, it is very important to validate the buyer’s VAT number before proceeding with the tax-exempt sale.
The most common cause of invalid VAT numbers are simple typing mistakes by the customer. To make the ordering process faster and friendlier, you should use a regular expression to validate the VAT number immediately while the customer fills out your online order form. You can do this with some client-side JavaScript or in the CGI script on your web server that receives the order form. If the number does not match the regular expression, the customer can correct the typo right away.
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 ...