有没有算法来验证澳大利亚TFN号码?

时间:2016-10-26 02:01:19

标签: javascript c# vb.net algorithm validation

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

Wikipedia page提到了一个简单的modulo-11算法,但它似乎只是一个例子。

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

在Swift 5中:

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

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的原始答案。

答案 2 :(得分:0)

支持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
    }
}