jsvat
Check the validity of the format of an EU VAT number. No dependencies.
What is it?
jsvat is a small library to check validity of European (and few non-eu) VAT number. (learn more about VAT) jsvat use 2-step check (see below) and didn't make any request for external resources.
Each country has own regexp for VAT number and different math-logic of number calculating.
What jsvat do?
Just check is VAT number valid or not and which country this VAT is:
jsvat.checkVAT('BG131134023');
jsvat.checkVAT('atu5-150-7409');
jsvat.checkVAT('aTU 5 804 4146');
Return value
jsvat.checkVAT()
returns Result
Object:
{
value: 'BG131134023', // VAT without extra characters (like '-' and spaces)
isValid: true,
country: { // VAT's couuntry (null if not found)
name: country.name, //Name of the country
isoCode: { //Country ISO codes
short: 'BE',
long: 'BEL',
numeric: '056' //String, because of forwarding zero
}
}
}
Allow or block countries
You can specify list of allowed countries
- Add some countries into
blocked
array:
jsvat.blocked = ['austria', 'Belgium', 'RU', '470'] //Can be country's name or iso code
jsvat.checkVAT('BG131134023') //result's isValid will be === false
To reset blocked
just do jsvat.blocked = [];
- Add some countries into
allowed
array:
jsvat.allowed = ['SK', 'Russia'] //All other countries would be blocked
To reset allowed
just do jsvat.allowed = [];
Important: it's not recommended to use blocked
and allowed
in same time. To stay on a safe side use one of them.
- Basically check result:
function allowOnlyBelgium(vat) {
var result = jsvat.checkVAT(vat)
return result.isValid && result.isoCode.short === 'BE'
}
It's better to use comparison with isoCode
instead of name cause some countries can have multiple variations of name
(Netherlands aka Dutch, UK aka England, etc)
Installation
- Bower
bower i jsvat --save
- NPM (node.js)
npm i jsvat --save
- Directly download one of the latest releases:
https://github.com/se-panfilov/jsvat/releases
- Just use
jsvat.chcekVat(vat)
from global scope. If you didn't like global scope - wrap it'
How to use jsvat?
It's simple:
jsvat.checkVAT(vat); //returns Object
vat
param means VAT number (string
), like "BG0433170001".
vat
can be passed with '-' (BG0-4331-70001
) or ' ' (space, like BG 0433 17 0001
) characters;
How does jsvat check the validity?
There is 2-step check:
- Compare with list of Regexps;
For example regexp for austria is /^(AT)U(\d{8})$/
.
Looks like ATU99999999
is valid (it's satisfy the regexp), but actually it's should be invalid.
- Some magic mathematical counting;
Here we make some mathematical calculation (different for each country).
After that we may be sure that ATU99999999
and for example ATV66889218
isn't valid, but ATU12011204
is valid.
NOTE: VAT numbers of some countries should ends up with special characters. Like '01' for Sweden or "L" for Cyprus. If 100% real VAT doesn't fit, try to add proper appendix.
List of supported Countries:
- Austria
- Belgium
- Bulgaria
- Switzerland
- Cyprus
- Czech Republic
- Germany
- Denmark
- Greece
- Spain
- Europe
- Estonia
- Finland
- France
- United Kingdom
- Croatia
- Hungary
- Ireland
- Italy
- Latvia
- Lithunia
- Luxembourg
- Malta
- Netherlands
- Norway
- Poland
- Portugal
- Romania
- Russia Federation
- Serbia
- Slovenia
- Slovakia republic
- Sweden
What if I don't need all countries
You can do your own custom build.
- Download or clone;
- Remove extra countries (that you don't) need from
src/countries
; - Build it
gulp build
(don't forget to makenpm i
first);
Versions for frameworks:
- Angular-jsvat (for angular 1.x.x)
Browsers Supports
Support all browsers down to IE9 (including IE9).
Changelog
#####1.2.0
- Added more info regarding countries in result (
isoCodes
,name
)
#####1.1.0
- jsvat now always return Object (there is no more just true or false value);
- Changed way of jsvat configuration (instead of object with countries, now you should pass an array with list of allowed countries);
LICENSE
MIT: https://github.com/se-panfilov/jsvat/blob/master/LICENSE