我正在用javascript制作个人身份号码程序, 它是这样的:
const kontrollTecken = "0123456789ABCDEFHJKLMNPRSTUVWXY";
function test(){
var toTest = document.getElementById("input").value;
document.getElementById("message").innerHTML="";
document.getElementById("info").classList.remove("hidden");
if(toTest.length == 11 ||toTest.length == 10){
var controlVal = toTest.substring(0,6) + toTest.substring(7,10);
var control = parseInt(controlVal) % 31;
var arhundrade;
switch (toTest[6]) {
case "a":
case "A":
arhundrade= "2000";
break;
case "-":
arhundrade= "1900";
break;
case "+":
arhundrade= "1800";
break;
default:
arhundrade= "Okänt"; //Unknown
break;
}
if(typeof kontrollTecken[control] !=='undefined'){
document.getElementById("kontrolltecken").innerHTML = kontrollTecken[control] == toTest[10] ? kontrollTecken[control] : "Borde vara//should be " + kontrollTecken[control];
document.getElementById("arhundrade")innerHTML = arhundrade;
document.getElementById("kon").innerHTML = (toTest[9] % 2) == 0 ? "Kvinna" : "Man"; //woman - man
var dateString = arhundrade.substring(0, 2) + toTest.substring(4, 6) + "-" + toTest.substring(2, 4) + "-" + toTest.substring(0, 2);
var bornAge = new Date (dateString);
document.getElementById("alder").innerHTML = isValidDate(bornAge) ? getAge(bornAge) + "år" : "Ogiltig födelsedag!";
}else{
document.getElementById("info").classList.add("hidden");
document.getElementById("message").innerHTML = "Felaktig Personsignum";
}
}else{
document.getElementById("info").classList.add("hidden");
document.getElementById("message").innerHTML = toTest.length < 11 ? "För lite tecken" : "För många tecken"; // Too few numbers / too many numbers
}
}
function isValidDate (d) {
return d instanceof Date && !isNaN (d);
}
function getAge(dateString)
{
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))
{
age--;
}
return age;
}
*All of it seems to work but when someone who is born in 1900
说无效的出生年份,有人知道为什么吗? *此系统基于芬兰的系统,因此,如果您出生于1900年,则您的电话号码由“-”组成。 https://en.wikipedia.org/wiki/National_identification_number arhundrade代表Century。