savagenoob Posted November 9, 2010 Share Posted November 9, 2010 Here is the JS function CheckCardNumber(form) { var tmpyear; if (form.CardNumber.value.length == 0) { alert("Please enter a Card Number."); form.CardNumber.focus(); return false; } if (form.ExpYear.value.length == 0) { alert("Please enter the Expiration Year."); form.ExpYear.focus(); return false; } if (form.ExpYear.value > 96) tmpyear = "19" + form.ExpYear.value; else if (form.ExpYear.value < 21) tmpyear = "20" + form.ExpYear.value; else { alert("The Expiration Year is not valid."); return false; } tmpmonth = form.ExpMon.options[form.ExpMon.selectedIndex].value; if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) { alert("This card has already expired."); return false; } card = form.CardType.options[form.CardType.selectedIndex].value; var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value + "\", " + tmpyear + ", " + tmpmonth + ");"); cardname = ""; if (retval) return true; else { for (var n = 0; n < Cards.size; n++) { if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) { cardname = Cards[n].getCardType(); break; } } if (cardname.length > 0) { alert("This looks like a " + cardname + " number, not a " + card + " number."); } else { alert("This card number is not valid."); return false; } } } I put a onsubmit = "CheckCardNumber(this.form)" on the form, but it submits regardless if the info is correct or not. Maybe you can spot my error, I cant. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 9, 2010 Share Posted November 9, 2010 Do the debug dance. Scatter alerts() throughout this code to make sure all the variables are what you expect them to be. It's far easier to ask the computer "what's in this variable" than it is to ask the internet to debug code linked to a form they cannot see. -Dan Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 10, 2010 Share Posted November 10, 2010 I put a onsubmit = "CheckCardNumber(this.form)" on the form, but it submits regardless if the info is correct or not. Maybe you can spot my error, I cant. That's funny, because the error is this: onsubmit = "CheckCardNumber(this.form)" You need to use "return" in the onsubmit function call, like this <form onsubmit="return CheckCardNumber(this.form);" target=""> Otherwise the true/false you return from the validation function is not used. I make that mistake all the time. Although debugging is always a good idea, it probably wouldn't have helped you in this instance because at every step the code would probably have the value you expected right up to the last step of returning true/false. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 10, 2010 Share Posted November 10, 2010 You're using eval, why? var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value + "\", " + tmpyear + ", " + tmpmonth + ");"); Just create one function that takes an additional parameter for the card type. Or if you want to keep the logic separated between functions, then jsut create one primary function that takes all the parameters plus the card type, then use a switch statement in that function to call the appropriate validation function. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.