Ejemplo n.º 1
0
        /// <summary>
        /// Validates a VAT number
        /// </summary>
        /// <param name="vat">The VAT (with country identification) of a registered company</param>
        /// <returns>VatValidationResult</returns>
        public static VatValidationResult IsValid(string vat)
        {
            vat = vat.Sanitize();

            if (string.IsNullOrWhiteSpace(vat))
            {
                return(VatValidationResult.Failed("VAT number cannot be null or empty."));
            }

            if (vat.Length < 3)
            {
                return(VatValidationResult.Failed($"VAT number '{vat}' is too short."));
            }

            var countryCode = vat.Substring(0, 2);

            if (!EUVatValidators.TryGetValue(countryCode, out var validator))
            {
                return(VatValidationResult.Failed($"{countryCode} is not a valid ISO_3166-1 European member state."));
            }

            var vatNumber = vat.Substring(2);

            return(validator.Validate(vatNumber));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="vat"></param>
        /// <returns></returns>
        /// <exception cref="ViesValidationException"></exception>
        public VatValidationResult Validate(string vat)
        {
            if (Regex == null)
            {
                throw new ViesValidationException("The regex to validate format is null.");
            }

            return(!Regex.IsMatch(vat)
                ? VatValidationResult.Failed($"Invalid {CountryCode} vat: format")
                : OnValidate(vat));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Validates a given country code and VAT number
 /// </summary>
 /// <param name="countryCode">The two-character country code of a European member country</param>
 /// <param name="vatNumber">The VAT number (without the country identification) of a registered company</param>
 /// <returns>VatValidationResult</returns>
 public static VatValidationResult IsValid(string countryCode, string vatNumber)
 {
     return(!EUVatValidators.TryGetValue(countryCode, out var validator)
         ? VatValidationResult.Failed($"{countryCode} is not a valid ISO_3166-1 European member state.")
         : validator.Validate(vatNumber));
 }