DEV Community

Saverio Bertocci
Saverio Bertocci

Posted on

Why Regex Is Never Enough for Italian Forms (And How to Fix It with an API)

If you've ever built a checkout form or a CRM for the Italian market, you know the struggle.

You ask the user for a phone number, an address, or a VAT Number (Partita IVA), and you get a wild mix of formats. People write "v.le" instead of "Viale", add random spaces in their phone numbers, and type 10 digits for a VAT number instead of 11.

The standard developer reaction is to write a massive Regex. But here is the problem: Regex is not enough.

The "Modulo 10" Problem

For example, the Italian VAT Number (Partita IVA) is 11 digits long. A simple /^[0-9]{11}$/ regex will let any random string of 11 numbers pass.
However, the Italian Revenue Agency uses the Luhn Algorithm (Modulo 10) to validate VAT numbers. The 11th digit is actually a control character calculated mathematically from the first 10.

If you don't validate it mathematically, your database will be filled with fake or mistyped VAT numbers.

The Solution: Offload the dirty work

I got tired of copy-pasting the Modulo 10 algorithm and address-cleaning functions into every new Node.js project. So, during the weekend, I decided to pack all these rules into a single micro-service.

I built the Italian Data Normalizer API.

It takes messy inputs like this:


json
{
  "street": "v.le trastevere 10",
  "city": "ROMA",
  "province": "rm",
  "zip": "153"
}

And returns beautifully formatted data, calculating the Modulo 10 for VATs and cleaning the strings:

{
  "street": "Viale Trastevere, 10",
  "city": "Roma",
  "province": "RM",
  "zip": "00153"
}

Try it for free
Instead of keeping it private, I published it on RapidAPI. There is a free tier (100 requests/month) which is more than enough for testing or small projects.

You don't even have to write the fetch requests yourself. I made a tiny open-source wrapper in JavaScript.

Check out the wrapper on GitHub and grab your free API key from the README:
👉 https://github.com/x4v1er94/italian-data-utils-js.git

I'd love to hear your feedback. Try to break it with weird inputs and let me know if I missed any edge cases!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)