We've been asked to validate a Canadian SIN in an LF Forms
The rules for validation are:
I've found this snippet of code on the web but don't know how too adapt it to work inside Forms for Validation of the SIN field. If any coders out there want to take a crack at this, Canadian Form makers will be very happy :)
Thanks
/**
* Validate a Canadian Social Insurance Number (SIN)
* @param {num || str} sin A 9-digit Canadian SIN
* @return {bool} Validity of the input SIN
*/
function validateSIN (sin) {
var check, even, tot;
if (typeof sin === 'number') {
sin = sin.toString();
}
if (sin.length === 9) {
// convert to an array & pop off the check digit
sin = sin.split('');
check = +sin.pop();
even = sin
// take the digits at the even indices
.filter(function (_, i) { return i % 2; })
// multiply them by two
.map(function (n) { return n * 2; })
// and split them into individual digits
.join('').split('');
tot = sin
// take the digits at the odd indices
.filter(function (_, i) { return !(i % 2); })
// concatenate them with the transformed numbers above
.concat(even)
// it's currently an array of strings; we want numbers
.map(function (n) { return +n; })
// and take the sum
.reduce(function (acc, cur) { return acc + cur; });
// compare the result against the check digit
return check === (10 - (tot % 10)) % 10;
} else throw sin + ' is not a valid sin number.';
}