How to validate a VAT number in Javascript

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"
}
Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store