Git Product home page Git Product logo

jsvat's Introduction

Codacy Badge bitHound Overall Score bitHound Code Code Climate Build Status Bower version npm version devDependency Status GitHub license

NPM

jsvat

[Demo and Examples]

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

  1. 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 = [];

  1. 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.

  1. 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

  1. Bower

bower i jsvat --save

  1. NPM (node.js)

npm i jsvat --save

  1. Directly download one of the latest releases:

https://github.com/se-panfilov/jsvat/releases

  1. 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:

  1. 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.

  1. Some magic mathematical counting;

Here we make some mathematical calculation (different for each country). After that we may be sure that ATU99999999and for example ATV66889218 isn't valid, but ATU12011204 is valid.

List of supported Countries:

  • Austria
  • Belgium
  • Bulgaria
  • Switzerland
  • Cyprus
  • Czech Republic
  • Germany
  • Denmark
  • Greece
  • Spain
  • Europe
  • 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.

  1. Download or clone;
  2. Remove extra countries (that you don't) need from src/countries;
  3. Build it gulp build (don't forget to make npm i first);

Versions for frameworks:

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

jsvat's People

Contributors

greenkeeperio-bot avatar se-panfilov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.