I am building an addition to a shopping cart I have developed. We are going to accept payment from invoices, not just cc or "paying when you get the package from the post office".
In Sweden people and organisations have a special number in this pattern: 112233-4455
If it's a person the firts six numbers is the date of birth.
The number uses the last digit as a check, and the rest is calculated by multiplying with 2 or 1 starting from the first number, like this:
1*2 1*1 2*2 2*1 3*2 3*1 4*2 4*1 5*2
If the result is greater than 9, the two didgits are separated.
Then all the results are added like this:
2+1+4+2+6+3+8+4+1+0 (5*2=10 wich gives 1+0)
The result is then checked to see what is left to get a zero on the end:
31 needs 9 to get to 40 (zero on the end)
Check the result to the control to see if it's a valid number:
Check digit is 5, result is 9, this is not a valid number!
So... programming this isn't easy, so I decided to see if someone else had done something similar. I found a script written in javascript and it isn't exactly what I want, but I know I can adapt it, IF I can convert it to PHP.
I need your help. I have marked the parts I will use and adapt, so unless it's extremely woven into it, the other parts don't need to be translated. (I have searched the net to find a converter, but so far, no luck)
Script:
PHP Code:
// Validerar personnummer. 12 siffror. Inga bindestreck. Koll av sista siffran.
//(Validates personal number. 12 digits. No hyphen. Check of last digit.)
function PersonnummerOK(pnr) {
var ok = true;
// var answer = "";
var length_pnr = pnr.length;
trmp_nr = 0;
del_nr = 0;
sum_nr = 0;
temp_str = "";
ej_nr = false;
// Kontroll av att fältet ej är tomt
//(Controll: Is field empty?)
if (pnr == "") {
return false
// answer += "tomt" //Tomt = Empty
}
// Kontroll av att fältet innehåller exakt 12 tecken
//(Does field have exactly 12 numbers?)
if (length_pnr != 12) {
return false
// answer += "\n fel längd: " + length_pnr
}
// Loopa igenom alla tecken och kontrollera att de är siffror
//(Loop through all signs, make sure they're digits.)
for (i=1; i < 13; i++) {
ej_nr = isNaN(parseInt(pnr.substring((i-1),i)));
if (ej_nr == true) {
return false;
// alert("tecken nr:" + (i-1) +"är ej en siffra " + pnr.substring((i-1),i))
};
};
// Kontrollera att de två första siffrorna är 19.
//(Control that first didgits are 19)
if (pnr.substring(0,2) != "19") {
return false
// answer += "\n Börjar ej med 19: " + pnr.substring(0,2)
}
// Beräkning av korrekt personnummer.
//(Calculate correct number).
//This is where the part starts I think I need to use.
// Loopa igenom tecken 3 till 11 (dvs personnumret utan 19 i början och exklusive sista tecknet) och summera
//(Loop through numbers 3 to 11 and add).
for (i=3; i < 12; i++) {
temp_nr = parseInt(pnr.substring((i-1),i));
// Om i är udda tal multipliceras med 2, annars med 1 (If uneven, multiply with 2, otherwise with 1)
if (i % 2 == 0) {
sum_nr += temp_nr
} else {
// Om 2*talet blir tvåsiffrigt summeras de två siffrorna för sig
//(If 2 * number is 2 digits, add numbers separatly)
if ((2*temp_nr) > 9) {
del_nr = parseInt((temp_nr*2)/10);
sum_nr += del_nr + ((2*temp_nr) - (del_nr*10))
} else {
sum_nr += 2*temp_nr
}
}
}
// Kontroll av att sista siffra stämmer med den uträknade
//(Check the last didgit to the calculated one)
temp_nr = 0;
temp_nr = parseInt((sum_nr + 10)/10)*10 - sum_nr;
if (temp_nr == 10) temp_nr = 0;
if (temp_nr != parseInt(pnr.substring(11,12))) {
return false;
};
// alert("Personnumret är OK")
return ok;
};
This script is only for personal numbers written in this format: 195903123456 and that doesn't fit with organisational numbers. Besides, most people are used to write their personal number like this: 590312-3456, and the check for 19 doesn't work now that we have reached 2000.