gpt4 book ai didi

javascript - 是否有任何算法来验证澳大利亚 TFN 号码?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:06 26 4
gpt4 key购买 nike

我们希望验证客户输入的澳大利亚 TFN 号码。有没有在某处提到任何官方算法?

Wikipedia page提到了一个简单的模 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

在 C# 中:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com