首页 > 代码库 > 【HAVENT原创】Australian Business Number (ABN) 验证
【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) 验证
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。