Looking for more info? Follow our learning guides and get started!

Validate a SIN


/* * Function to check if a given number is a valid SIN (Social Insurance Number) * or not. * A SIN is 9 digit number. */ // sample SIN number var num= "130-692-544"; //var num= "123-456-782"; var result = isSIN(num); if (result) document.write(num + " is a [valid] SIN." ); else document.write(num + " is [not a valid] SIN." ); function isSIN(num) { // check for length 9 digits + 2 hyphen if (num.length != 11) { return false; } var a = 0, b = 0; var j = 0; // for odd /even digits tracker var checkDigit = num[10]; // loop through all the digits of the number // as the last digit is the check number. for (var i=0; i< num.length -1; i++) { var ch = num[i]; //document.write(ch); if (ch== "-") { // skip it } else { j++; // even or add digit if (j % 2 == 0) { // even digit var db = parseInt(ch) * 2; // add each digit dad = parseInt(db/10) + db % 10; a = a + dad; // document.write("a=" +dad + "<br>"); } else { // odd digit b = b + parseInt(ch); //document.write("b=" + parseInt(ch) + "<br>"); } } } document.write("<br>"); // substract the sets var c = a + b ; var d = 10 - c %10; document.write("a=" + a +",b=" + b +",c=" + c +",d=" +d +",cd=" + checkDigit + "<br>"); if (d == checkDigit) { return true; } else { return false; } }

Loading Please Wait...