Jump to content

CC Validation Help


savagenoob

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.