Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    2,065

    Question Converting javascript to php - need some help, please

    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=113i++) {
            
    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=312i++) {
            
    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 (== 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 == 10temp_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.
    "Stop flame-wars - Report a post"
    The original Kitty Lizard

  2. #2
    Join Date
    Dec 2003
    Location
    Tampa Area FL
    Posts
    27
    Hello,

    Im not a big javascript person but i do know PHP very profficiently.

    give me a second and ill whip you something up

  3. #3
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    2,065
    Great!

  4. #4
    Join Date
    Dec 2003
    Location
    Tampa Area FL
    Posts
    27
    This should do the trick if i understand exactly what it is you are trying to do

    PHP Code:
    <?PHP

    function validate_personal_num($number)
    {
        
    // get length of string
        
    $length strlen($number); 
        
    $length--;
        
        
        
    // make the string an array of each integer.
        
    for($i 0$i <= $length$i++)
        {
            if(
    is_numeric($number{$i}))
            {
                
    $nums_array[] = $number{$i};
            }
        }
        
        
    $j count($nums_array)-2;
        
    $multiplier 2;
        
        
    $total_count 0;
        
        for(
    $i 0$i <= $j$i++)
        {
            
    $total = ($nums_array[$i] * $multiplier);
            if(
    $total <= 9)
            {
                
    $total_count += $total;
            }
            else
            {
                
    $total_count += + ($total -10);
            }
            
            
            
    // check the multiplier and reverse it.
            
    if($multiplier == 2)
            {
                
    $multiplier 1;
            }
            else
            {
                
    $multiplier 2;
            }
        } 
        
        
    // now check to see if $total_count + check number is divisible by 10
        
    $i count($nums_array) - 1;
        
    $check_number $nums_array[$i];
        if((
    $total_count $check_number) % 10 != 0)
        {
            return 
    FALSE;
        }
        else
        {
            return 
    TRUE;
        }
        
    }

    $number "112233-4455";
    if(!
    validate_personal_num($number))
    {
        echo 
    "incorrect number supplied";
    }
    ?>
    Last edited by PHPConcepts; 12-01-2003 at 10:44 AM.
    Erik S.
    Developer / Owner

  5. #5
    Join Date
    Dec 2003
    Location
    Tampa Area FL
    Posts
    27
    i take it everything worked out ok?

    if not let me know what needs to be done differently
    Erik S.
    Developer / Owner

  6. #6
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    2,065
    Thanks! I will check it out. Sorry I didn't answer earlier, but my tiemzone must be different from yours and I'm at work when I'm on-line.


    I just ran ut, and it works great Thank you so much.

    I know there are so many different ways of approaching a problem. Your approach seem very simple, and is probably something similar to what I would have come up with, if I hadn't been stuck on trying to translate the javascript I found.
    Last edited by Reptilian Feline; 12-02-2003 at 04:27 AM.
    "Stop flame-wars - Report a post"
    The original Kitty Lizard

  7. #7
    Join Date
    Dec 2003
    Location
    Tampa Area FL
    Posts
    27
    Im glad i could help
    Erik S.
    Developer / Owner

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •