验证 ABN(澳大利亚商业号码)

本文关键字:号码 澳大利亚 ABN 验证 | 更新日期: 2023-09-27 18:00:14

我需要一些现代C#代码来检查澳大利亚商业号码(ABN(是否有效。

粗略的要求是

  • 用户已输入 ABN
  • 允许在任何时候使用空格以使数字可读
  • 如果包含除数字和空格之外的任何其他字符 - 即使包含合法的数字序列,验证也应失败
  • 此检查是调用 ABN 搜索 Web 服务的先兆,该服务将保存输入明显错误的调用
验证

数字的确切规则在 abr.business.gov.au 指定,为了简洁明了,此处省略了这些规则。规则不会随着时间的推移而改变。

验证 ABN(澳大利亚商业号码)

这是基于 Nick Harris 的示例,但清理为使用现代 C# 习语

/// <summary>
/// http://stackoverflow.com/questions/38781377
/// 1. Subtract 1 from the first (left) digit to give a new eleven digit number         
/// 2. Multiply each of the digits in this new number by its weighting factor         
/// 3. Sum the resulting 11 products         
/// 4. Divide the total by 89, noting the remainder         
/// 5. If the remainder is zero the number is valid          
/// </summary>
public bool IsValidAbn(string abn)
{
    abn = abn?.Replace(" ", ""); // strip spaces
    int[] weight = { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
    int weightedSum = 0;
    //0. ABN must be 11 digits long
    if (string.IsNullOrEmpty(abn) || !Regex.IsMatch(abn, @"^'d{11}$"))
    {
        return false;
    }
    //Rules: 1,2,3                                  
    for (int i = 0; i < weight.Length; i++)
    {
        weightedSum += (int.Parse(abn[i].ToString()) - (i == 0 ? 1 : 0)) * weight[i];
    }
    //Rules: 4,5                 
    return weightedSum % 89 == 0;
}

奖励 xUnit 测试,让您的技术主管满意...

[Theory]
[InlineData("33 102 417 032", true)]
[InlineData("29002589460", true)]
[InlineData("33 102 417 032asdfsf", false)]
[InlineData("444", false)]
[InlineData(null, false)]
public void IsValidAbn(string abn, bool expectedValidity)
{
    var sut = GetSystemUnderTest();
    Assert.True(sut.IsValidAbn(abn) == expectedValidity);
}

试试这个:

var abn = "51 824 753 556";
var weighting = new [] { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
var isValid =
    abn
        .Where(x => char.IsDigit(x))
        .Select((x, n) => (x - '0') - (n == 0 ? 1 : 0))
        .Zip(weighting, (d, w) => d * w)
        .Sum() % 89 == 0;

IsValid true输入 ABN 是否有效或false

我的 JavaScript 版本验证 ABN 和 ACN

荷兰:

function isValidABN(num) {
    const weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
    // Convert to string and remove all white space
    num = num.toString().replace(/'s/g, "");
    // Split it to number array
    num = num.split('').map(n => parseInt(n));
    // Subtract 1 from the first (left-most) digit of the ABN to give a new 11 digit number
    num[0] = num[0] - 1;
    // Multiply each of the digits in this new number by a "weighting factor" based on its position as shown in the table below
    num = num.map((n, i) => n * weights[i]);
    // Sum the resulting 11 products
    let total = num.reduce((acc, n) => {
        return acc + n;
    }, 0);
    // Divide the sum total by 89, noting the remainder
    if(total % 89 === 0) {
        return true;
    } else {
        return false;
    }
}

乙腈:

function isValidACN(num) {
    const weights = new Array(8, 7, 6, 5, 4, 3, 2, 1);
    // Convert to string and remove all white space
    num = num.toString().replace(/'s/g, "");
    // Split it to number array
    num = num.split('').map(n => parseInt(n));
    // Set the check digit and remove it 
    let checkDigit = num.pop();
    // Apply weighting to digits 1 to 8.
    num = num.map((n, i) => n * weights[i]);
    // Sum the products
    let total = num.reduce((acc, n) => {
        return acc + n;
    }, 0);
    // Divide by 10 to obtain remainder
    let calculatedCheckDigit = (10 - (total % 10)) % 10;
    // calculatedCheckDigit should match check digit
    if(calculatedCheckDigit === checkDigit) {
        return true;
    } else {
        return false;
    }
}

Python

def isValidABN(ABN = None):
    if(ABN == None):    return False
    ABN = ABN.replace(" ","")
    weight = [10,1,3,5,7,9,11,13,15,17,19]
    weightedSum = 0
    if(len(ABN) != 11): return False
    if(ABN.isnumeric() == False): return False
    i = 0    
    for number in ABN:
        weightedSum += int(number) * weight[i]
        i += 1
    weightedSum -= 10 #This is the same as subtracting 1 from the first digit.
    result = weightedSum % 89 == 0
    return result