Is there a way to transform this function into an AngularJS
directive for real-time validation of user input? I'm encountering an error when trying to use it as a regular function in the controller:
"Error: pesel.substring is not a function"
function decode(pesel)
{
var year=parseInt(pesel.substring(0,2),10);
var month = parseInt(pesel.substring(2,4),10)-1;
var day = parseInt(pesel.substring(4,6),10);
if(month>80) {
year = year + 1800;
month = month - 80;
}
else if(month > 60) {
year = year + 2200;
month = month - 60;
}
else if (month > 40) {
year = year + 2100;
month = month - 40;
}
else if (month > 20) {
year = year + 2000;
month = month - 20;
}
else {
year += 1900;
}
var birthday=new Date();
birthday.setFullYear(year, month, day);
var weights = [9,7,3,1,9,7,3,1,9,7];
var sum = 0
for(var i=0;i<weights.length;i++) {
sum+=(parseInt(pesel.substring(i,i+1),10) * weights[i]);
}
sum=sum % 10;
var valid=(sum===parseInt(pesel.substring(10,11),10));
//sex
if(parseInt(pesel.substring(9,10),10) % 2 === 1) {
var sex='m';
} else {
var sex='f';
}
return {valid:valid,sex:sex,date:birthday};
}