如何验证新加坡FIN?

mja*_*day 3 algorithm validation

任何人都可以提供验证新加坡FIN的算法吗?

我知道新加坡的NRIC我可以通过模11验证它,然后将结果与查询表进行比较,但找不到FIN的类似查找表.

我也不确定模11是否是正确的验证方法.

我知道政府出售400美元的算法,但也许有人知道更便宜的方式.

c#实现的奖励积分.

mja*_*day 6

再次在PHP中

function isNricValid ($theNric) {

    $multiples = array( 2, 7, 6, 5, 4, 3, 2 );

    if (!$theNric || $theNric == '')
    {
        return false;
    }

    if (strlen($theNric) != 9)
    {
        return false;
    }

    $total = 0;
    $count = 0;
    $numericNric = 0;

    $first = $theNric[0];
    $last = $theNric[strlen($theNric) - 1];

    if ($first != 'S' && $first != 'T')
    {
        return false;
    }

    $numericNric = substr($theNric, 1, strlen($theNric) - 2);

    if (!is_numeric ($numericNric)) {
        return false;
    }

    while ($numericNric != 0)
    {
        $total += ($numericNric % 10) * $multiples[sizeof($multiples) - (1 + $count++)];

        $numericNric /= 10;
        $numericNric = floor($numericNric);
    }

    $outputs = '';
    if (strcmp($first, "S") == 0)
    {
        $outputs = array( 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' );
    }
    else
    {   
        $outputs = array( 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'J', 'Z', 'I', 'H' );
    }

    return $last == $outputs[$total % 11];

}

function isFinValid ($fin)
{
    $multiples = array( 2, 7, 6, 5, 4, 3, 2 );
    if (!$fin || $fin == '')
    {
        return false;
    }

    if (strlen($fin) != 9)
    {
        return false;
    }

    $total = 0;
    $count = 0;
    $numericNric = 0;
    $first = $fin[0];
    $last = $fin[strlen($fin) - 1];

    if ($first != 'F' && $first != 'G')
    {
        return false;
    }

    $numericNric = substr($fin, 1, strlen($fin) - 2);

    if (!is_numeric ($numericNric)) {
        return false;
    }

    while ($numericNric != 0)
    {
        $total += ($numericNric % 10) * $multiples[sizeof($multiples) - (1 + $count++)];

        $numericNric /= 10;
        $numericNric = floor($numericNric);
    }

    $outputs = array();

    if (strcmp($first, 'F') == 0)
    {
        $outputs = array( 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' );
    }
    else
    {
        $outputs = array( 'R', 'Q', 'P', 'N', 'M', 'L', 'K', 'X', 'W', 'U', 'T' );
    }

    return $last == $outputs[$total % 11];
}
Run Code Online (Sandbox Code Playgroud)


mja*_*day 5

这是用JavaScript编写的类似代码

var nric = [];

nric.multiples = [ 2, 7, 6, 5, 4, 3, 2 ];

nric.isNricValid = function (theNric) {

    if (!theNric || theNric == '')
    {
        return false;
    }

    if (theNric.length != 9)
    {
        return false;
    }

    var total = 0
        , count = 0
        , numericNric;
    var first = theNric[0]
        , last = theNric[theNric.length - 1];

    if (first != 'S' && first != 'T')
    {
        return false;
    }

    numericNric = theNric.substr(1, theNric.length - 2);

    if (isNaN(numericNric)) {
        return false
    }

    while (numericNric != 0)
    {
        total += (numericNric % 10) * nric.multiples[nric.multiples.length - (1 + count++)];

        numericNric /= 10;
        numericNric = Math.floor(numericNric);
    }

    var outputs;
    if (first == 'S')
    {
        outputs = [ 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' ];
    }
    else
    {
        outputs = [ 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'J', 'Z', 'I', 'H' ];
    }

    return last == outputs[total % 11];

}

nric.isFinValid = function(fin)
{
    if (!fin || fin == '')
    {
        return false;
    }

    if (fin.length != 9)
    {
        return false;
    }

    var total = 0
        , count = 0
        , numericNric;
    var first = fin[0]
        , last = fin[fin.length - 1];

    if (first != 'F' && first != 'G')
    {
        return false;
    }

   numericNric = fin.substr(1, fin.length - 2);

    if (isNaN(numericNric)) {
        return false;
    }

    while (numericNric != 0)
    {
        total += (numericNric % 10) * nric.multiples[nric.multiples.length - (1 + count++)];

        numericNric /= 10;
        numericNric = Math.floor(numericNric);
    }

    var outputs;
    if (first == 'F')
    {
        outputs = [ 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' ];
    }
    else
    {
        outputs = [ 'R', 'Q', 'P', 'N', 'M', 'L', 'K', 'X', 'W', 'U', 'T' ];
    }

    return last == outputs[total % 11];
}
Run Code Online (Sandbox Code Playgroud)


mja*_*day 4

经过一番搜索后,我找到了一种验证它们的方法。这并不一定意味着 FIN 有效,只是它落在有效范围内。

我基于它的算法http://www.ngiam.net/NRIC/ppframe.htm的算法

我还提供了一种类似的方法来检查身份证,因为我认为任何遇到此方法并对其中一种感兴趣的人也会对另一种感兴趣。

希望这对某人有帮助!

    private static readonly int[] Multiples = { 2, 7, 6, 5, 4, 3, 2 };

    public static bool IsNricValid(string nric)
    {
        if (string.IsNullOrEmpty(nric))
        {
            return false;
        }

        //  check length
        if (nric.Length != 9)
        {
            return false;
        }

        int total = 0
            , count = 0
            , numericNric;
        char first = nric[0]
            , last = nric[nric.Length - 1];

        if (first != 'S' && first != 'T')
        {
            return false;
        }

        if (!int.TryParse(nric.Substring(1, nric.Length - 2), out numericNric))
        {
            return false;
        }

        while (numericNric != 0)
        {
            total += numericNric % 10 * Multiples[Multiples.Length - (1 + count++)];

            numericNric /= 10;
        }

        char[] outputs;
        if (first == 'S')
        {
            outputs = new char[] { 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' };
        }
        else
        {
            outputs = new char[] { 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'J', 'Z', 'I', 'H' };
        }

        return last == outputs[total % 11];

    }

    public static bool IsFinValid(string fin)
    {
        if (string.IsNullOrEmpty(fin))
        {
            return false;
        }

        //  check length
        if (fin.Length != 9)
        {
            return false;
        }

        int total = 0
            , count = 0
            , numericNric;
        char first = fin[0]
            , last = fin[fin.Length - 1];

        if (first != 'F' && first != 'G')
        {
            return false;
        }

        if (!int.TryParse(fin.Substring(1, fin.Length - 2), out numericNric))
        {
            return false;
        }

        while (numericNric != 0)
        {
            total += numericNric % 10 * Multiples[Multiples.Length - (1 + count++)];

            numericNric /= 10;
        }

        char[] outputs;
        if (first == 'F')
        {
            outputs = new char[] { 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' };
        }
        else
        {
            outputs = new char[] { 'R', 'Q', 'P', 'N', 'M', 'L', 'K', 'X', 'W', 'U', 'T' };
        }

        return last == outputs[total % 11];
    }
Run Code Online (Sandbox Code Playgroud)