3 Replies - 695 Views - Last Post: 06 April 2013 - 10:58 AM

#1 glopez810  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-April 13

credit card validator problem

Posted 05 April 2013 - 05:30 PM

this is the famous credit card validator using the luhn algorithm... my code is not finished but i'm really close and i've hit bump. i keep getting NaN from my sum of even indexed array values (s1). i'm actually not even sure if i'm doing it correctly. what's wrong with it?

<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
    <title>exercise 4 part 2</title>
    <script type="text/javascript">
    
      function fixString(){
        
        //sums of digits
        var s1, s2 = 0;
        
        //get credit card number
        var ccNumber = document.getElementById("ccNumber").value;
        
        //remove hyphens and spaces
        var modifiedNumber = ccNumber.replace(/-/g, "").replace(/\s/g, "");
        
        //split into array
        var myArray = modifiedNumber.split(""); 
        
        //reverse array
        myArray.reverse(); 
        
        //sum even indexed numbers
        for(i = 0; i < myArray.length; ++i){
       
          var c = parseInt(myArray[i]);
          
            if (i % 2 !=0)
            c *=2;
          s1 += c;
        }
       
        
        
        show.innerHTML = s1;
        
      }
      
    </script>
  </head>
  <body>
    <form action="#">
      <p><label>Enter credit card number here:<input id="ccNumber" type="text">
        </label> <input value="Validate" onclick="fixString()" type="button"> </p>
    </form>
    <p id="show"></p>
  </body>
</html>

        


Is This A Good Question/Topic? 0
  • +

Replies To: credit card validator problem

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: credit card validator problem

Posted 05 April 2013 - 05:48 PM

var s1 = 0, s2 = 0;

Was This Post Helpful? 0
  • +
  • -

#3 glopez810  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-April 13

Re: credit card validator problem

Posted 06 April 2013 - 10:40 AM

View Postandrewsw, on 05 April 2013 - 05:48 PM, said:

var s1 = 0, s2 = 0;


wow thats embarrassing...

well anyway, i finished this, however, i am getting invalids on card numbers that should be valid. where did my calculations go wrong?


<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
    <title>exercise 4 part 2</title>
    <script type="text/javascript">
    
      function fixString(){
        
        //sums of digits
        var s1 = 0;
        var s2 = 0;
        var s3 = 0;
        
        //get credit card number
        var ccNumber = document.getElementById("ccNumber").value;
        
        //remove hyphens and spaces
        var modifiedNumber = ccNumber.replace(/-/g, "").replace(/\s/g, "");
        
        //split into array
        var myArray = modifiedNumber.split(""); 
        
        //reverse array
        myArray.reverse(); 
        
        //sum numbers
        // C IS THE CURRENT INDEX
        for(i = 0; i < myArray.length; ++i){
       
          var c = parseInt(myArray[i]);
          
            if (i % 2 !=0)
            c *=2;
          s1 += c;
        }
       
       
        for (i=0; i < myArray.length; ++i){
        
           var c = parseInt(myArray[i]);
          
          s2 += c; 
        
        }
        
        s3 = s1+s2; 
       
        
        //CHECK
        if (s3 != 0 && s3 % 10 == 0)
          show.innerHTML = "VALID";
         else
            show.innerHTML = "INVALID";
          
        
      }
      
    </script>
  </head>
  <body>
    <form action="#">
      <p><label>Enter credit card number here:<input id="ccNumber" type="text">
        </label> <input value="Validate" onclick="fixString()" type="button"> </p>
    </form>
    <p id="show"></p>
  </body>
</html>


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: credit card validator problem

Posted 06 April 2013 - 10:58 AM

I don't know. You will have to take some credit-card numbers, work out the correct numbers for them, and walk through your code. Use alerts or console.log() to assist. There are links for consoles in my signature, and you would benefit if you learnt how to set a breakpoint and step through your code.

BTW I would remove all non-numbers, and then check the length of the remaining value, before performing these calculations.

var modifiedNumber = ccNumber.replace(/[^0-9]/g, "");

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1