'Greek VAT validation number code

Is there anybody which can assist me with a code in VB.NET visual studio 2010 for validating Greek TAX Registration number (VAT).



Solution 1:[1]

It's not EU laws that makes it 9 digits, it depends on the country.

Unfortunately I don't know if you can get hold of the algorithm anywhere and even if you could, that still wouldn't mean that it was a valid VAT number. The only way that I know of to be sure that it's a valid VAT number is to validate it against the webservice as mentioned in Rup's comment. So if you're going to use this validation to decide if you're going to charge VAT or not, I'd not trust just a calculation since then you might end up breaking the rules (and possibly having to pay VAT that you haven't collected...).

There's a code project article showing how to use it (C# but should be relatively easy to convert to VB.Net if needed): VIES - VAT number checker

Though obviously it might be worthwhile checking for 9 digits first to rule out any obviously invalid ones.

Solution 2:[2]

As it is mentioned here you won't get the complete validation because "the European Commission cannot divulge these algorithms". However greece Vat must have a 9 digits block. So it might suffice to check this with a Regex:

Dim text As String = Me.TxtVAT.Text
Dim regex As New System.Text.RegularExpressions.Regex("^\d{9}$", System.Text.RegularExpressions.RegexOptions.Compiled)
If regex.IsMatch(text) Then
   'do something'
Else
   'do something else'
End If

Solution 3:[3]

I found a JavaScript implementation of the checksum algorithm which references this site as the original algorithm source:

function ELVATCheckDigit (vatnumber) {
  // Checks the check digits of a Greek VAT number.

  var total = 0;
  var multipliers = [256,128,64,32,16,8,4,2];

  //eight character numbers should be prefixed with an 0.
  if (vatnumber.length == 8) {vatnumber = "0" + vatnumber};

  // Extract the next digit and multiply by the counter.
  for (var i = 0; i < 8; i++)
    total = total + Number(vatnumber.charAt(i)) * multipliers[i];

  // Establish check digit.
  total = total % 11;
  if (total > 9) {total = 0;};  

  // Compare it with the last character of the VAT number. If it is the same, 
  // then it's a valid check digit.
  if (total == vatnumber.slice (8,9)) 
    return true
  else 
    return false;
}

but that only checks it's in a valid format, not if the number has actually been allocated. As above, you can use the web service for that:

  • right-click on your project and 'Add Service References'; enter the URL http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl and a namespace name
  • instantiate a checkVatPortTypeClient from that namesspace
  • call client.checkVat. You'll need to put the country code and VAT number into string variables for it to reference, and provide output variables for the validation flag, the company name and address.

Solution 4:[4]

For Greek Tax Number Validation you could use https://tin-check.com/

They have a API that returns the response in JSON https://api.tin-check.com/

May be a good solution if beside greek tax number you need to perform validation for more countries. They perform validation for more than 100 countries.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hans Olsson
Solution 2 Tim Schmelter
Solution 3 Rup
Solution 4 N Martins