C# 验证ABN(澳大利亚商业编号)
我需要一些现代的C#代码来检查澳大利亚商业号码(ABN)是否有效 这些需求是松散的C# 验证ABN(澳大利亚商业编号),c#,validation,C#,Validation,我需要一些现代的C#代码来检查澳大利亚商业号码(ABN)是否有效 这些需求是松散的 ABN已由用户输入 允许在任意点使用空格使数字可读 如果包含数字和空格以外的任何其他字符-即使包含合法的数字序列,验证也应失败 此检查是调用的前兆,它将保存输入明显错误的呼叫 为简洁明了起见,此处省略了验证数字的确切规则。这些规则不会随着时间的推移而改变。这是基于示例,但经过清理后使用了现代C#习惯用法 试试这个: var abn = "51 824 753 556"; var weighting = ne
- ABN已由用户输入
- 允许在任意点使用空格使数字可读
- 如果包含数字和空格以外的任何其他字符-即使包含合法的数字序列,验证也应失败
- 此检查是调用的前兆,它将保存输入明显错误的呼叫
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
是输入ABN的true
或false
是否有效。验证ABN和ACN的我的JavaScript版本
荷兰银行:
ACN:
蟒蛇
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
isValid&=weightedSum%89==0代码>---这里的&=
运算符的用途是什么?另外-您可以在abn.Replace(“,”)
之前移动空检查,或者删除整个!string.IsNullOrWhiteSpace(abn)
完全是多余的。我喜欢它!已作相应修订。不过,我让它更能原谅空输入。消费者可以改变口味偏好一点旁注:abn[i]-“0”可以用来代替int.Parse(abn[i].ToString())
请不要链接到外部站点,除非该链接用于支持您已经提出问题的内容。如果澳大利亚政府改变了链接,那么问题就消失了。令人印象深刻的简洁,但是没有通过null
和33 102 417 032ASDFSFSF
@fiat的单元测试输入-谢谢。您的问题不要求验证null
和33 102 417 032ASDFSFSF
。它要求我们验证ABN(一个11位数字)。事实上,我过滤的数字超出了问题的要求。您认为非ABN输入的规则应该是什么?在我的例子中,ABN是用户类型的,这将在MVC验证属性中使用。为了可读性,11位数字通常用空格表示,因此11位数字可以用任意数量的空格表示。用户添加alpha字符的任何内容都应该完全拒绝。此检查是调用gov.au ABN搜索Web服务的前兆。@fiat-您应该在问题中添加额外的详细信息。我的ABN验证和ABN号码屏蔽使用它
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;
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;
}
}
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