Class VATINValidator

java.lang.Object
org.apache.commons.validator.routines.VATINValidator

public class VATINValidator extends Object
VAT identification number (VATIN) Validator.

The validator includes a default set of formats and check routines for European Union countries, see https://en.wikipedia.org/wiki/VAT_identification_number

This can be adjusted f.i. adding a new country routines by creating a validator and using the setValidator(String, int, String, CheckDigit) method to add (or remove) an entry.

For example:

 VATINValidator v = new VATINValidator();
 v.setValidator("GB", 14, "GB(\\d{3})?\\d{9}", VATidGBCheckDigit.getInstance());
 

The singleton default instance cannot be modified in this way.

Since:
1.10.0
  • Field Details

    • DEFAULT_VATIN_VALIDATOR

      public static final VATINValidator DEFAULT_VATIN_VALIDATOR
      The singleton instance which uses the default formats
  • Constructor Details

    • VATINValidator

      public VATINValidator()
      Create a default validator.
    • VATINValidator

      public VATINValidator(VATINValidator.Validator[] validators)
      Create an VATIN validator from the specified map of VATIN formats.
      Parameters:
      validators - map of VATIN formats
  • Method Details

    • getInstance

      public static VATINValidator getInstance()
      Gets the singleton instance of the VATIN validator using the default formats
      Returns:
      A singleton instance of the validator
    • getDefaultValidators

      public VATINValidator.Validator[] getDefaultValidators()
      Gets a copy of the default Validators.
      Returns:
      a copy of the default Validator array
    • getValidator

      public VATINValidator.Validator getValidator(String code)
      Gets the Validator for a given VATIN
      Parameters:
      code - a string starting with the ISO country code (e.g. "BG831650349" Nestlé.bg)
      Returns:
      the validator or null if there is no one registered.
    • hasValidator

      public boolean hasValidator(String code)
      Does the class have the required validator?
      Parameters:
      code - the code to check
      Returns:
      true if there is a validator for the country
    • isValid

      public boolean isValid(String code)
      Validate a VATIN Code
      Parameters:
      code - The value validation is being performed on (e.g. "BG831650349" Nestlé.bg)
      Returns:
      true if the value is valid
    • setValidator

      public VATINValidator.Validator setValidator(String countryCode, int length, String format, CheckDigit routine)
      Installs a validator. Will replace any existing entry which has the same countryCode.
      Parameters:
      countryCode - the country code
      length - the length of the VATIN. Must be ≥ 8 and ≤ 32. If the length is < 0, the validator is removed, and the format is not used.
      format - the format of the VATIN for the country (as a regular expression)
      routine - the CheckDigit module
      Returns:
      the previous Validator, or null if there was none
      Throws:
      IllegalArgumentException - if there is a problem
      IllegalStateException - if an attempt is made to modify the singleton validator
    • setValidator

      public VATINValidator.Validator setValidator(VATINValidator.Validator validator)
      Installs a validator. Will replace any existing entry which has the same countryCode
      Parameters:
      validator - the instance to install.
      Returns:
      the previous Validator, or null if there was none
      Throws:
      IllegalStateException - if an attempt is made to modify the singleton validator