【问题标题】:Is there a known way to validate an Estonian business ID?是否有一种已知的方法来验证爱沙尼亚的企业 ID?
【发布时间】:2021-07-21 13:54:17
【问题描述】:

我必须验证爱沙尼亚的企业 ID(不是公民 ID)。 ID是9个数字,但我认为可能有一个分配它们的系统。例如,这里是芬兰验证,最后一个数字作为校验和

if (preg_match('/^\d{7}-\d{1}$/', $user_input)) {
    list($num, $control) = preg_split('[-]', $user_input);
    // Add leading zeros if number is < 7
    $num         = str_pad($num, 7, 0, STR_PAD_LEFT);
    $controlSum  = 0;
    $controlSum += (int)substr($num, 0, 1)*7;
    $controlSum += (int)substr($num, 1, 1)*9;
    $controlSum += (int)substr($num, 2, 1)*10;
    $controlSum += (int)substr($num, 3, 1)*5;
    $controlSum += (int)substr($num, 4, 1)*8;
    $controlSum += (int)substr($num, 5, 1)*4;
    $controlSum += (int)substr($num, 6, 1)*2;
    $controlSum  = $controlSum%11;
    if ($controlSum == 0) {
        return ($controlSum == $control) ? true : false;
    } elseif ($controlSum >= 2 && $controlSum <= 10 ) {
        return ((11 - $controlSum) == $control) ? true : false;
    }
}

通常所有这些类型的 ID 都有一个校验和,所以这不仅仅是在其上运行正则表达式的问题,而且我找不到任何与爱沙尼亚企业相关的内容。

任何指向已知库的链接,示例将不胜感激。我正在使用 PHP,但这些可以是任何语言。

谢谢。

【问题讨论】:

    标签: php validation checksum


    【解决方案1】:

    您可以包含来自欧盟委员会的 SOAP-Call https://ec.europa.eu/taxation_customs/vies/vatRequest.html

    它应该像这样工作(不是 100% 测试):

    $vatId = "{{the vat id }}";
    
    $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
    if ($client) {
         $cc           = substr($vatId, 0, 2);
         $vn           = substr($vatId, 2);
         $params       = array('countryCode' => $cc, 'vatNumber' => $vn);
         $result = $client->checkVat($params);
         if ($result->valid == true) {
            echo "VAT-ID ok";
         }
    } else {
        die("connection failed");
    }
    

    【讨论】:

    • 谢谢,这是个好主意,但我不认为我可以在应用程序中使用第三方服务。很遗憾他们不能发布源代码。
    【解决方案2】:

    我找到了这个库,它回答了我的问题

    https://github.com/arthurdejong/python-stdnum/blob/master/stdnum/ee/registrikood.py

    from stdnum.exceptions import *
    from stdnum.util import clean, isdigits
    
    def calc_check_digit(number):
        """Calculate the check digit."""
        check = sum(((i % 9) + 1) * int(n)
                    for i, n in enumerate(number[:-1])) % 11
        if check == 10:
            check = sum((((i + 2) % 9) + 1) * int(n)
                        for i, n in enumerate(number[:-1])) % 11
        return str(check % 10)
    
    def compact(number):
        """Convert the number to the minimal representation. This strips the
        number of any valid separators and removes surrounding whitespace."""
        return clean(number, ' ').strip()
    
    
    def validate(number):
        """Check if the number provided is valid. This checks the length and
        check digit."""
        number = compact(number)
        if not isdigits(number):
            raise InvalidFormat()
        if len(number) != 8:
            raise InvalidLength()
        if number[0] not in '1789':
            raise InvalidComponent()
        if number[-1] != calc_check_digit(number):
            raise InvalidChecksum()
        return number
    
    
    def is_valid(number):
        """Check if the number provided is valid. This checks the length and
        check digit."""
        try:
            return bool(validate(number))
        except ValidationError:
            return False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多