HAVENT原创Australian Business Number (ABN) 验证

Posted 夜雨流星℡?

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HAVENT原创Australian Business Number (ABN) 验证相关的知识,希望对你有一定的参考价值。

php 代码如下:

//   ValidateABN
//     Checks ABN for validity using the published 
//     ABN checksum algorithm.
//
//     Returns: true if the ABN is valid, false otherwise.
//      Source: http://www.clearwater.com.au/code
//      Author: Guy Carpenter
//     License: The author claims no rights to this code.  
//              Use it as you wish.
 
function ValidateABN($abn)
{
    $weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
 
    // strip anything other than digits
    $abn = preg_replace("/[^\d]/","",$abn);
 
    // check length is 11 digits
    if (strlen($abn)==11) {
        // apply ato check method 
        $sum = 0;
        foreach ($weights as $position=>$weight) {
            $digit = $abn[$position] - ($position ? 0 : 1);
            $sum += $weight * $digit;
        }
        return ($sum % 89)==0;
    } 
    return false;
}

 

javascript 代码如下:

function checkABN(str) {
    if (!str || str.length !== 11) {
        return false;
    }
    var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
        checksum = str.split(‘‘).map(Number).reduce(
        function(total, digit, index) {
            if (!index) {
                digit--;
            }
            return total + (digit * weights[index]);
        },
        0
    );

    if (!checksum || checksum % 89 !== 0) {
        return false;
    }

    return true;
}

 

C# 代码如下:

public static bool ValidateABN(string abn)
{
    bool isValid = false;
    int[] weight = { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
    int weightedSum = 0;

    //ABN must not contain spaces, comma‘s or hypens
    abn = StripNonDigitData(abn);

    //ABN must be 11 digits long  
    if (!string.IsNullOrEmpty(abn) & Regex.IsMatch(abn, "^\\d{11}$"))
    {
        //Rules: 1,2,3  
        for (int i = 0; i <= weight.Length - 1; i++)
        {
            weightedSum += (int.Parse(abn[i].ToString()) - ((i == 0 ? 1 : 0))) * weight[i];
        }
        //Rules: 4,5  
        return ((weightedSum % 89) == 0);
    }
    return isValid;
}

public static string StripNonDigitData(string input)
{
    StringBuilder output = new StringBuilder("");
    foreach (char c in input)
    {
        if (char.IsDigit(c))
        {
            output.Append(c);
        }
    }
    return output.ToString();
}

 

ABNLookup 查询(需要去 https://abr.business.gov.au/Documentation/WebServiceRegistration 注册一个账号,如果成功,会在5天内收到一封邮件,里面有个GUID),PHP 代码:

<?php

namespace PPost\Library;

class ABNLookup extends \SoapClient{

    private $guid = "xxxxxxxxxxxxxxxxxx";    // guid
    
    public function __construct()
    {
        $params = array(
            ‘soap_version‘ => SOAP_1_1,
            ‘exceptions‘ => true,
            ‘trace‘ => 1,
            ‘cache_wsdl‘ => WSDL_CACHE_NONE
        ); 

        parent::__construct(‘http://abr.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL‘, $params);
    }
    
    public function searchByAbn($abn, $historical = ‘N‘){
        $params = new \stdClass();
        $params->searchString                = $abn;
        $params->includeHistoricalDetails    = $historical;
        $params->authenticationGuid            = $this->guid;
        return $this->ABRSearchByABN($params);
    }
}
<?php

namespace PPost\Infrastructure\Helper;


class ABNHelper
{

    public static function ValidateABN($abn){
        $weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);

        // strip anything other than digits
        $abn = preg_replace("/[^\d]/","",$abn);

        // check length is 11 digits
        if (strlen($abn)==11) {
            // apply ato check method
            $sum = 0;
            foreach ($weights as $position=>$weight) {
                $digit = $abn[$position] - ($position ? 0 : 1);
                $sum += $weight * $digit;
            }
            return ($sum % 89)==0;
        }

        return false;
    }

    public static function ABNLookup($abn){
        try{
            $abnLookup = new \PPost\Library\ABNLookup();
            try{
                $result = $abnLookup->searchByAbn($abn);

                var_dump($result);
                //return $result->ABRPayloadSearchResults->response;
                return $result;
            } catch    (Exception $e){
                throw $e;
            }
        } catch(Exception $e){
            throw $e;
        }
    }
}

 

以上是关于HAVENT原创Australian Business Number (ABN) 验证的主要内容,如果未能解决你的问题,请参考以下文章

HAVENT原创Salesforce 给字段加上链接

HAVENT原创修改 CentOS 服务器名称

HAVENT原创centOS 下 nginx 配置和启动

HAVENT原创Docker 创建一个新的 Node 镜像,并发布到 DockerHub

UVA10142 Australian Voting序列处理

POJ2692 Australian Voting序列处理