<!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>
credit card validator problem
Page 1 of 13 Replies - 1075 Views - Last Post: 06 April 2013 - 10:58 AM
#1
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?
Replies To: credit card validator problem
#2
Re: credit card validator problem
Posted 05 April 2013 - 05:48 PM
var s1 = 0, s2 = 0;
#3
Re: credit card validator problem
Posted 06 April 2013 - 10:40 AM
andrewsw, 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>
#4
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.
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, "");
Page 1 of 1