How to validate a VAT number in Javascript

Cloudmersive
2 min readMar 11, 2020

--

Dealing with invalid value added tax identification numbers (VAT) can really drive you to the drink. There is an easy fix to this problem, however, which we are going to take a look at in this post. Stay tuned!

So instead of going to all the trouble of setting up VAT validation the good ol’ fashioned way, we are instead going to whip out an API to do it all for us instead. This requires installing our API client using this script tag.

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-validate-client.js"></script>

Once that has finished, go ahead and call the function CloudmersiveValidateApiClient, as you can see here.

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveValidateApiClient.VatApi();var input = new CloudmersiveValidateApiClient.VatLookupRequest(); // VatLookupRequest | Input VAT codevar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.vatVatLookup(input, callback);

Easy. Now our data about the input VAT number will be given back in the following format:

{
"CountryCode": "string",
"VatNumber": "string",
"IsValid": true,
"BusinessName": "string",
"BusinessAddress": "string",
"BusinessBuilding": "string",
"BusinessStreetNumber": "string",
"BusinessStreet": "string",
"BusinessCity": "string",
"BusinessStateOrProvince": "string",
"BusinessPostalCode": "string",
"BusinessCountry": "string"
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet