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

本文关键字:TFN 号码 澳大利亚 验证 任何 算法 是否 | 更新日期: 2023-09-27 18:15:10

我们希望验证客户输入的澳大利亚TFN号码。有没有什么地方提到过官方的算法?

维基百科页面提到了一个简单的模11算法,但它似乎只是一个例子。

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

在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);
}

在Swift 5中:

确保使用键盘类型Number Pad来防止用户添加非数字字符。

func isTFNValid (tfn: String) -> Bool{
    if (tfn.count != 9) {
        return false
    } else {
        let characters = Array(tfn)
        let digits = characters.map { Int(String($0))!}
        //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 && tfn.count == 9) {
            return true
        } else {
            return false
        }
    }
}

感谢Jeremy Thompson的原创回答

支持Java 8和可能更高版本的实现

public class TFNUtils {
    public static String format(String tfn) {
        if (tfn == null) {
            return null;
        }
        tfn = tfn.replaceAll("''s+","");
        tfn = tfn.replaceAll("''-", "");
        if (tfn.length() != 9) {
            return null;
        }
        int sum = (Integer.parseInt(String.valueOf(tfn.charAt(0))))
                + (Integer.parseInt(String.valueOf(tfn.charAt(1)))*4)
                + (Integer.parseInt(String.valueOf(tfn.charAt(2)))*3)
                + (Integer.parseInt(String.valueOf(tfn.charAt(3)))*7)
                + (Integer.parseInt(String.valueOf(tfn.charAt(4)))*5)
                + (Integer.parseInt(String.valueOf(tfn.charAt(5)))*8)
                + (Integer.parseInt(String.valueOf(tfn.charAt(6)))*6)
                + (Integer.parseInt(String.valueOf(tfn.charAt(7)))*9)
                + (Integer.parseInt(String.valueOf(tfn.charAt(8)))*10);
        if ((sum % 11) != 0){
            return null;
        }
        StringBuilder tfnCompute = new StringBuilder(11);
        tfnCompute.append(tfn.charAt(0));
        tfnCompute.append(tfn.charAt(1));
        tfnCompute.append(tfn.charAt(2));
        tfnCompute.append('-');
        tfnCompute.append(tfn.charAt(3));
        tfnCompute.append(tfn.charAt(4));
        tfnCompute.append(tfn.charAt(5));
        tfnCompute.append('-');
        tfnCompute.append(tfn.charAt(6));
        tfnCompute.append(tfn.charAt(7));
        tfnCompute.append(tfn.charAt(8));
        return tfnCompute.toString();
    }
    public static boolean isValid(String tfn) {
        return format(tfn) != null;
    }
}
public class TFNUtilsTest {
    @Test
    public void testFormat() throws Exception {
        assertEquals("123-456-782", TFNUtilsTest.format("123-456-782"));
        assertEquals("123-456-782", TFNUtilsTest.format("123456782"));
        assertEquals("123-456-782", TFNUtilsTest.format("123 456 782"));
        // invaild returns null
        assertNull(TFNUtilsTest.format("123 456 7829"));
        assertNull(TFNUtilsTest.format("123 456 78"));
    }
    @Test
    public void testIsValid() throws Exception {
        assertTrue(TFNUtilsTest.isValid("123-456-782"));
        assertTrue(TFNUtilsTest.isValid("123 456 782"));
        assertFalse(TFNUtilsTest.isValid(null));
        assertFalse(TFNUtilsTest.isValid(""));
        assertFalse(TFNUtilsTest.isValid("123-456-780"));    // invalid check-digit
        assertFalse(TFNUtilsTest.isValid("123-456.782"));    // invalid seperator
        assertFalse(TFNUtilsTest.isValid("123-456-7820"));   //  > 9 digits
    }
}

我检查了这篇文章中共享的c#代码,实际上使用该代码生成的TFN号码在提到的网站中无法进行TFN验证。因此,我在这个网站https://clearwater.com.au/code/tfn的帮助下提出了另一种解决方案来生成有效的TFN。

试试这个代码:

static void Main(string[] args)
        {
            int count = 0;
            Random random = new Random();
            while (count < 50)
            {
                int randomNumber = random.Next(100000000, 999999999);
                // - Validate TFN - [START]
                float output;
                //validate only digits
                string strRandomNumber = randomNumber.ToString();
                if (!float.TryParse(strRandomNumber, out output))
                {
                    Console.WriteLine("Invalid Input");
                }
                else
                {
                    //validate length
                    if (strRandomNumber.Length != 9)
                    {
                        Console.WriteLine("Invalid Length");
                    }
                    else
                    {
                        int[] digits = Array.ConvertAll(strRandomNumber.ToArray(), c => (int)Char.GetNumericValue(c));
                        //do the calcs                   
                        var sum = (digits[0] * 10)
                                + (digits[1] * 7)
                                + (digits[2] * 8)
                                + (digits[3] * 4)
                                + (digits[4] * 6)
                                + (digits[5] * 3)
                                + (digits[6] * 5)
                                + (digits[7] * 2)
                                + (digits[8] * 1);
                        var remainder = sum % 11;
                        // - Validate TFN - [END]
                        if ((remainder == 0))
                        {
                            Console.WriteLine(randomNumber.ToString());
                            break;
                        }
                    }
                }
                count++;
            }
        }