Tuesday, July 30, 2013

NRIC(Singapore) Validation using Javascript

function IsValidNRIC(theNric) {
    var nric = [];
    nric.multiples = [2, 7, 6, 5, 4, 3, 2];

    if (theNric.length != 9) {
        alert('Invalid NRIC')
        return false;
    }

    var total = 0, count = 0, numericNric;
    var first = theNric.charAt(0), last = theNric.charAt(theNric.length - 1);

    if (first != 'S' && first != 's') {
        alert('Invalid NRIC S');
        return false;
    }
    /*Above is working*/

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

    if (isNaN(numericNric)) {
        alert('Invalid NRIC Middle Not a number')
        return false
    }

    if (numericNric != null) {
        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'];
    }

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

    if (last != outputs[total % 11]) {
        alert('Invalid end Character')
        return false;
    }

    nric.isNricValid = function (theNric) {
        if (!theNric || theNric == '') {
            return false;
        }
    }
    return true;
}

No comments:

Post a Comment

MS CRM 2011 KB Article customization Issue.

Recently I have encountered some issue while customizing Kb Article Entity. I was doing following configuration in Article form. 1. Add Ba...