JavaScript card PAN check digit Luhn verification
credit-card, javascript, luhn
Solution
move `onsubmit="Validate(Luhn);"`
to the form tag and pass the form
Like this - note I pass the form and find the number from the form. I also moved the test and return false/return true around
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)">
Problem
I've used the code from the link below to try and validate a credit card, however I'm not getting an alert when I submit a the wrong data in the field. Strip spaces before performing Luhn 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?