JavaScript PAN Check Digit Luhn Check

I used the code from the link below to try to verify a credit card, however I do not get a warning when I send the wrong data to the field.

Mileage Bands Before Performing a Moon Check

my form is as follows:

<form id="myform" method="post" action=""> <p>Select credit card: <select tabindex="11" id="CardType"> <option value="AmEx">American Express</option> <option value="CarteBlanche">Carte Blanche</option> <option value="DinersClub">Diners Club</option> <option value="Discover">Discover</option> <option value="EnRoute">enRoute</option> <option value="JCB">JCB</option> <option value="Maestro">Maestro</option> <option value="MasterCard">MasterCard</option> <option value="Solo">Solo</option> <option value="Switch">Switch</option> <option value="Visa">Visa</option> <option value="VisaElectron">Visa Electron</option> <option value="LaserCard">Laser</option> </select> </p> <p> Enter number: <input type="text" id="CardNumber" maxlength="24" size="24" /> <input type="submit" id="submitbutton" onsubmit="Validate(Luhn);" /> </p> </form> 

Maybe I'm using the wrong code?

+4
javascript credit-card luhn
source share
2 answers

move onsubmit="Validate(Luhn);"

in the form tag and submit the form

Like this is a note. I pass the form and find the number from the form. I also transferred the test and returned false / return true

http://jsfiddle.net/mplungjan/VqXss/

 function Validate(theForm) { var Luhn = theForm.CardNumber.value; var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length)); var LuhnLess = Luhn.substring(0,Luhn.length-1); if (Calculate(LuhnLess)!=parseInt(LuhnDigit)) { alert("\n\nYou have mis-typed your card number! \nPlease check and correct.\n\n") return false; } return true; } </script> </head> <body> <form id="myform" method="post" action="" onsubmit="return Validate(this)"> 
+2
source share

I came to this question by looking for an online PAN validator for an online map in javascript that can be safely used to check a PAN check digit without the risk of malicious interception on the server.

There are many Luhn javascript implementations at http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#JavaScript and https://sites.google.com/site/abapexamples/javascript/luhn-validation .

Here is a typical implementation:

 var LuhnCheck = (function() { var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]; return function(str) { var counter = 0; var incNum; var odd = false; var temp = String(str).replace(/[^\d]/g, ""); if ( temp.length == 0) return false; for (var i = temp.length-1; i >= 0; --i) { incNum = parseInt(temp.charAt(i), 10); counter += (odd = !odd)? incNum : luhnArr[incNum]; } return (counter%10 == 0); } })(); 

And using googling for "luhn jsfiddle" I found another one ready to use a reliable online validator:

http://jsfiddle.net/silvinci/84bru/light/

+1
source share

All Articles