social insurance number: only 9 digits (1-9), only number, should be input in three boxes, 3 digit in each boxes( if possible use auto tab)
add the odd digits : means 1th,3dr,5th,7th, 9th digits together = odd digits sum value
Extract the even digits: mean 2nd, 4th, 6th, 8th digits-- and multiply by *2, add all the even digits together only if it is less than 10
if the 2nd, 4th, 6th, 8th digits is greater than 10 then , add like this ( for eg the 4th digit is 18 then add= 1+8)
Then,
odd digits sum value + the even digit sum value = multiple of ten (%10)
<!DOCTYPE >
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function Validation(sin)
{
var num = sin.match(/^(\d{3})-?\d{3}-?\d{3}$/);
var numDashes = sin.split('-').length - 1;
if (num == null || numDashes == 1) {
alert('Invalid S.I.N. Must be 9 digits or in the form NNN-NNN-NNN.');
msg = "does not appear to be valid";
}
else
if (parseInt(num[1], 10) == 0) {
alert("Invalid SIN: SIN's can't start with 000.");
msg = "does not appear to be valid";
}
else {
msg = "appears to be valid";
alert(sin + "\r\n\r\n" + msg + " Social Insurance Number.");
}
if (!num.test(sin)) return false;
sin = sin.split("-").join("");
var checksum=0;
for (var n = (2 - (sin.length % 2)); n <= sin.length; n += 2) {
checksum += parseInt(sin.chartAt(n - 1));
for (var n = (sin.length % 2) + 1; n < sin.length; n += 2)
var digit = parseInt(sin.charAt(n - 1)) * 2
if (digit < 10) {
checksum += digit;
}
else {
checksum += (digit - 9);
}
}
if ((checksum % 10) == 0) return true; else return false;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Social Insurance</title>
<style type="text/css">
#pge {width:400px;margin:10px auto;}
input {width:60px;text-align:center;}
</style>
</head>
<body>
<form id="pge"><fieldset><legend>Social Insuerance number</legend>
<p>Enter your 9 digit number in this cases</p>
<p><input id="sin0" type="text" onkeyup="checkNumber(this)"><input id="sin1" type="text" onkeyup="checkNumber(this)"><input id="sin2" type="text" onkeyup="checkNumber(this)"></p>
<p id="rsl"></p>
<input id='rst' type="reset" value="reset">
</fieldset></form>
<script type="text/javascript">
var nmbSin,arrSin;
function checkNumber(nptBox){var nxt,vle=nptBox.value,vlf=vle.replace(/\D/g,'');
nptBox.value=vlf;
if (nptBox.id=='sin0') nmbSin='';
if ((''+vlf).length==3) {nmbSin+=''+vlf;
if (nxt=nptBox.nextSibling) {nxt.value='';nxt.focus();}
else {// the test
arrSin=nmbSin.split('');
var sum=0,toAdd;
for (i=0;i<arrSin.length;i++) {toAdd=arrSin[i]*(1+i%2);sum+=toAdd<10?toAdd:toAdd-9;}
alert(arrSin+' '+sum);
document.getElementById('rst').focus();
if (sum%10) {document.getElementById('rsl').innerHTML='This number is not valid ! (sum is '+sum+')';return}
document.getElementById('rsl').innerHTML='This number sems valid !';
}
}
}
</script>
</body>
</html>
It should be possible to improve the script to allow some corrections...
It may be necessary to perform the test each time to invite the user: to complete or to modify his response (by displaying a message and returning the focus if necessary) or to continue with an approval mark (ok!).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Social Insurance</title>
<style type="text/css">
body{margin:0;padding:0;}
#pge {width:400px;margin:10px auto;}
input {width:60px;margin:1px;text-align:center;border:1px solid #000}
fieldset {border:1px solid #000;}
</style>
</head>
<body>
<form id="pge"><fieldset><legend>Social Insuerance number</legend>
<p>Enter your number in this cases</p>
<p><input type="text" onkeyup="checkNumber(event,this)"><input type="text" onkeyup="checkNumber(event,this)"><input type="text" onkeyup="checkNumber(event,this)"></p>
<p id="rsl"></p>
<input id='rst' type="reset" value="reset">
</fieldset></form>
<script type="text/javascript">
function checkNumber(e,t){
var kcd=e?e.which:event.keyCode;//alert(kcd)
if (!((47<kcd && kcd<58) || (95<kcd && kcd<106))) return;
// The value without other character than at most three digits
var vle=t.value,vlf=vle.replace(/\D/g,'').substr(0,3),nxt;
t.value=vlf;
if ((''+vlf).length==3) if (nxt=t.nextSibling) nxt.focus();
// the test
var i,obj=t.parentNode.childNodes[0],nmb='',anb;
do {nmb+=obj.value} while (obj=obj.nextSibling);
anb=nmb.split('');
if (anb.length<9) {document.getElementById('rsl').innerHTML='Complete your number';return}
var i,l=anb.length,sum=0,trm;
for (i=0;i<l;i++) {trm=anb[i]*(1+i%2);sum+=trm<10?trm:trm-9;}
if (sum%10) {document.getElementById('rsl').innerHTML='Not a valid Insurence number ! (key is '+sum+')';return}
document.getElementById('rsl').innerHTML='ok !';
}
</script>
</body>
</html>
Bookmarks