hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Converting javascript to php - need some help, please
Reply

Forum Jump

Converting javascript to php - need some help, please

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-01-2003, 06:37 AM
Reptilian Feline Reptilian Feline is offline
Web Hosting Master
 
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



Sponsored Links
  #2  
Old 12-01-2003, 09:49 AM
PHPConcepts PHPConcepts is offline
Newbie
 
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  
Old 12-01-2003, 09:56 AM
Reptilian Feline Reptilian Feline is offline
Web Hosting Master
 
Join Date: Jul 2002
Location: Sweden
Posts: 2,065
Great!

Sponsored Links
  #4  
Old 12-01-2003, 10:34 AM
PHPConcepts PHPConcepts is offline
Newbie
 
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";
}
?>

__________________
Erik S.
Developer / Owner


Last edited by PHPConcepts; 12-01-2003 at 10:44 AM.
  #5  
Old 12-01-2003, 04:22 PM
PHPConcepts PHPConcepts is offline
Newbie
 
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  
Old 12-02-2003, 04:13 AM
Reptilian Feline Reptilian Feline is offline
Web Hosting Master
 
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.

__________________
"Stop flame-wars - Report a post"
The original Kitty Lizard


Last edited by Reptilian Feline; 12-02-2003 at 04:27 AM.
  #7  
Old 12-02-2003, 10:18 PM
PHPConcepts PHPConcepts is offline
Newbie
 
Join Date: Dec 2003
Location: Tampa Area FL
Posts: 27
Im glad i could help

__________________
Erik S.
Developer / Owner

Reply

Related posts from TheWhir.com
Title Type Date Posted
Swarmify's New Approach to Content Delivery Uses Site Visitors to Accelerate Content Web Hosting News 2014-05-01 08:34:03
Nasuni Launches Cloud Mirroring Feature to Storage Solution Web Hosting News 2013-08-08 14:15:27
OpenStack and the AWS API Debate Web Hosting News 2014-05-09 12:40:29
Facebook's Recently Acquired Mobile App Platform Parse Launches Web Hosting for Developers Web Hosting News 2013-05-08 10:49:17
Web Host Brain Host Acquires Site Builder My Creative Site Designs Web Hosting News 2012-09-24 14:03:36


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?