Return to Snippet

Revision: 57894
at June 13, 2012 22:24 by MGHollander


Initial Code
/**
 * http://www.pfz.nl/wiki/invoer-validatie/#Geldig_BTW.2FVAT_nummer
 *
 * Look if the given input could be a legal VAT-number.
 * Accepts input with or without '.' between the numbers, must contain a County-code
 *
 * Note: This only checks the syntax, NOT IF THE VAT REALLY EXISTS OR IS ACTIVE!
 *
 * @author Sebastiaan Stok <[email protected]>
 *
 * @param string $psVatInput
 * @return bool
 */
function isVAT($psVatInput) {
    $psVatInput = trim($psVatInput);
    $psVatInput = str_replace('.', '', $psVatInput);
    $aVatMatch = array();

    // Common syntax
    if (!preg_match('/^([a-z]{2})[ ]*(.+)$/is', $psVatInput, $aVatMatch)) {
        return false;
    }

    $aVatMatch[1] = strtoupper($aVatMatch[1]);
    
    $functionName = 'isVAT_'. $aVatMatch[1];
    if (function_exists($functionName)) {
        return $functionName($psVatInput);
    }

    $aVatRegexes = array(
        'AT' => 'U[0-9]{8}',
        'BE' => '0[0-9]{9}',
        'BG' => '[0-9]{9,10}',
        'CY' => '[0-9]{8}[A-Za-z]',
        'CZ' => '[0-9]{8,10}',
        'DE' => '[0-9]{9}',
        'DK' => '[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[0-9]{2}',
        'EE' => '[0-9]{9}',
        'EL' => '[0-9]{9}',
        'ES' => '([A-Za-z0-9][0-9]{7}[A-Za-z0-9])',
        'FI' => '[0-9]{8}',
        'FR' => '[A-Za-z0-9]{2} ?[0-9]{9}',
        'GB' => '([0-9]{3} ?[0-9]{4} ?[0-9]{2}|[0-9]{3} ?[0-9]{4} ?[0-9]{2} ?[0-9]{3}|GD[0-9]{3}|HA[0-9]{3})',
        'HU' => '[0-8]{8}',
        'IE' => '[0-9][A-Za-z0-9+*][0-9]{5}[A-Za-z]',
        'IT' => '[0-9]{11}',
        'LT' => '([0-9]{9}|[0-9]{12})',
        'LU' => '[0-9]{8}',
        'LV' => '[0-9]{11}',
        'MT' => '[0-9]{8}',
        'NL' => '[0-9]{9}B[0-9]{2}',
        'PL' => '[0-9]{10}',
        'PT' => '[0-9]{9}',
        'RO' => '[0-9]{2,10}',
        'SE' => '[0-9]{12}',
        'SI' => '[0-9]{8}',
        'SK' => '[0-9]{10}',
    );

    if (!isset($aVatRegexes[$aVatMatch[1]]) || !preg_match('/^([a-z]{2})[ ]*'.$aVatRegexes[$aVatMatch[1]].'$/is', $psVatInput)) {
        return false;
    }

    return true;
}

/**
 * Checks whether or not the sofinummer provided passes the test of 11.
 * @version 2.0
 * @param $sofinummer The sofinummer to be checked.
 * @return boolean
 * @author Ivo Peters
 * @author Berry Langerak, al had Ivo hem als eerste goed :)
 **/
function isVAT_NL( $psVatInput ) {
    // remove spaces and dots
    $sVatnr = str_replace(array(' ', '.'), '', ($psVatInput));
    
    if (preg_match('#^NL([0-9]{9})B[0-9]{2}$#i', $sVatnr, $aMatch)) {
        $sVatnr = $aMatch[1];
        
        // lijst met nummers die qua check kloppen, maar toch niet geldig zijn
        $aInvalid = array( 
            '111111110', 
            '999999990', 
            '000000000',
        );
        
        if (strlen($sVatnr) != 9 || !ctype_digit($sVatnr) || in_array($sVatnr, $aInvalid)) {
            return false;
        }
        for ($i = 9, $som = -$sVatnr % 10; $i > 1; $i--) {
            $som += $i * $sVatnr{(9 - $i)};
        }
        
        return ($som % 11 == 0);
    }
    
    return false;
}

/**
 * Check if the number might be a belgian VAT-number.
 * Accepts input with or without '.' and spaces between the numbers,
 * spaces at begin and end of string are ignored 
 * it must contain a County-code (BE) at the start (case insensitive)
 * after the country code 9 or 10 digits follow.
 * if 10 digits are given, the first must be zero
 *
 * Note: This only checks the syntax, NOT IF THE VAT REALLY EXITS OR IS ACTIVE!
 *
 * @author Ivo Peters
 *
 * @param string $psVatInput
 * @return bool
 */
function isVAT_BE($psVatInput) {
    // remove spaces and dots
    $sVatnr = str_replace(array(' ', '.'), '', ($psVatInput));

    if (preg_match('#^BE0?[0-9]{9}$#i', $sVatnr)) {
        $iVatnr = str_ireplace('BE','', $sVatnr);
        return (97 - (floor($iVatnr / 100) % 97) == $iVatnr % 100);
    }
    
    return false;
}

/**
 * Check if the number might be a GB VAT-number.
 * Accepts input with or without '.' and spaces between the numbers,
 * spaces at begin and end of string are ignored 
 * it must contain a County-code (GB) at the start (case insensitive)
 * after the country code 9 digits follow.
 *
 * Note: This only checks the syntax, NOT IF THE VAT REALLY EXITS OR IS ACTIVE!
 *
 * @author Ivo Peters
 *
 * @param string $psVatInput
 * @return bool
 */
function isVAT_GB($psVatInput) {
    // remove spaces and dots
    $sVatnr = str_replace(array(' ', '.'), '', ($psVatInput));

    if (preg_match('#^GB[0-9]{9}$#i', $sVatnr)) {
        $sVatnr = str_ireplace('GB', '', $sVatnr);
        $iSum = 0;
        for ($i = 0; $i < 7; $i++) {
            $iSum += substr($sVatnr, $i, 1) * (8 - $i);
            echo substr($sVatnr, $i, 1) .' * '. (8 - $i) .' = '. substr($sVatnr, $i, 1) * (8 - $i); 
            echo  PHP_EOL;
        }

        return (97 - $iSum % 97 == $sVatnr % 100);
    }
    
    return false;
}

Initial URL


Initial Description
Look if the given input could be a legal VAT-number.
Accepts input with or without '.' between the numbers and must contain a County-code

Initial Title
EU VAT Validation

Initial Tags
validation

Initial Language
PHP