我们希望验证我们的客户输入的澳大利亚TFN数字。
有没有提到什么官方算法?
Wikipedia page提到了一个简单的modulo-11算法,但它似乎只是一个例子。
最佳答案
在javascript中:
var tfn = $('#tfn').val();
//remove spaces and update
tfn = tfn.replace(/\s+/g, '');
$('#tfn').val(tfn);
//remove hyphens and update
tfn = tfn.replace(/[-]/g, '');
$('#tfn').val(tfn);
//validate only digits
var isNumber = /^[0-9]+$/.test(tfn);
if(!isNumber) {
return doError('Invalid TFN, only numbers are allowed.');
}
//validate length
var length = tfn.length;
if(length != 9) {
return doError('Invalid TFN, must have 9 digits.');
}
var digits = tfn.split('');
//do the calcs
var sum = (digits[0]*1)
+ (digits[1]*4)
+ (digits[2]*3)
+ (digits[3]*7)
+ (digits[4]*5)
+ (digits[5]*8)
+ (digits[6]*6)
+ (digits[7]*9)
+ (digits[8]*10);
var remainder = sum % 11;
if(remainder == 0) {
doSuccess('Valid TFN, hooray!');
} else {
return doError('Invalid TFN, check the digits.');
}
参考:https://github.com/steveswinsburg/tfn-validator/blob/master/tfn-validator.html
在C35中
static void Main(string[] args)
{
int count = 0;
StringBuilder sb = new StringBuilder();
Random random = new Random();
while (count < 500000) {
int randomNumber = random.Next(100000000, 999999999);
if (ValidateTFN(randomNumber.ToString()))
{
sb.AppendLine(randomNumber.ToString());
count++;
}
}
System.IO.File.WriteAllText("TFNs.txt", sb.ToString());
}
public static bool ValidateTFN(string tfn)
{
//validate only digits
if (!IsNumeric(tfn)) return false;
//validate length
if (tfn.Length != 9) return false;
int[] digits = Array.ConvertAll(tfn.ToArray(), c => (int)Char.GetNumericValue(c));
//do the calcs
var sum = (digits[0] * 1)
+ (digits[1] * 4)
+ (digits[2] * 3)
+ (digits[3] * 7)
+ (digits[4] * 5)
+ (digits[5] * 8)
+ (digits[6] * 6)
+ (digits[7] * 9)
+ (digits[8] * 10);
var remainder = sum % 11;
return (remainder == 0);
}
public static bool IsNumeric(string s)
{
float output;
return float.TryParse(s, out output);
}
关于javascript - 是否有任何算法可以验证澳大利亚TFN号码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40252533/