jillallynwilson
Programmer
I have the following script which is executed from a button:
function SubmitForm()
{
if (!(ValidateCreditCard('Payment'))) {
document._Payment.submit();
}
}
The form is only to submit, if it passes the 'ValidateCreditCard' check, however it is submitting the form even when it does not pass the validation check. Below is the script for 'ValidateCreditCard'. Can anyone tell me what I am doing wrong. Thank you.
// Validate Credit Card information
function ValidateCreditCard (pf) {
if (pf == "Payment") {
var pf = document._Payment;
}
now = new Date();
alert("Now: " + now);
var month = now.getMonth();
alert("Month: " + month);
var year = now.getFullYear();
alert("Year: " + year);
switch(month) {
case 0:
var currmonth = "01";
break;
case 1:
var currmonth = "02";
break;
case 2:
var currmonth = "03";
break;
case 3:
var currmonth = "04";
break;
case 4:
var currmonth = "05";
break;
case 5:
var currmonth = "06";
break;
case 6:
var currmonth = "07";
break;
case 7:
var currmonth = "08";
break;
case 8:
var currmonth = "09";
break;
case 9:
var currmonth = "10";
break;
case 10:
var currmonth = "11";
break;
case 11:
var currmonth = "12";
break;
}
alert("Month (new): " + currmonth);
if (pf.PaymentMethod.value == "Select") {
alert("Payment Method is required.");
pf.PaymentMethod.focus();
return false;
}
if (pf.CreditCardNumber.value == "") {
alert("Credit Card Number is required.");
pf.CreditCardNumber.focus();
return false;
}
if (pf.CreditCardNumber.value != "") {
if (!IsCreditCard(pf.CreditCardNumber)){
alert("Credit Card Number is not valid.");
pf.CreditCardNumber.focus();
return false;
}
}
alert("Exp Yr: " + pf.ExpirationYear.value);
if (pf.ExpirationYear.value < year) {
alert("Expiration Year must be equal to or greater than current year.");
pf.ExpirationYear.focus();
return false;
}
alert("Exp Month: " + pf.ExpirationMonth.value);
if (pf.ExpirationYear.value == year) {
if (pf.ExpirationMonth.value < currmonth) {
alert("Expiration Month must be equal to or greater than current month.");
pf.ExpirationMonth.focus();
return false;
}
}
if (pf.FirstName.value == "") {
alert("First Name as it appears on the card is required.");
pf.FirstName.focus();
return false;
}
if (pf.LastName.value == "") {
alert("Last Name as it appears on the card is required.");
pf.LastName.focus();
return false;
}
if (pf.CardID.value == "") {
alert("Card ID is required. Refer to the help icon for assistance.");
pf.CardID.focus();
return false;
}
}
function IsCreditCard (s) {
//alert("Is it a valid Credit Card?");
validChars = "1234567890";
var st = "";
sum = 0;
mul = 1;
if (s){
s = s.value ;
l = s.length;
// strip all characters that are not valid numbers
for (i = 0; i < l; i++) {
var c = s.charAt(i);
if (validChars.indexOf(c) != -1) {
st += c;
}
}
//alert("Stripped credit card number :" + st);
if (st.length == 0) {
return false;
} else {
mul = 1;
l = st.length;
// validate credit card number using Luhn Mod-10 test
for (i = 0; i < l; i++) {
digit = st.substring(l-i-1,l-i);
tproduct = parseInt(digit ,10)*mul;
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
mul++;
else
mul--;
}
if ((sum % 10) == 0) {
return true;
} else {
return false;
}
}
} else {
return true;
}
}
function SubmitForm()
{
if (!(ValidateCreditCard('Payment'))) {
document._Payment.submit();
}
}
The form is only to submit, if it passes the 'ValidateCreditCard' check, however it is submitting the form even when it does not pass the validation check. Below is the script for 'ValidateCreditCard'. Can anyone tell me what I am doing wrong. Thank you.
// Validate Credit Card information
function ValidateCreditCard (pf) {
if (pf == "Payment") {
var pf = document._Payment;
}
now = new Date();
alert("Now: " + now);
var month = now.getMonth();
alert("Month: " + month);
var year = now.getFullYear();
alert("Year: " + year);
switch(month) {
case 0:
var currmonth = "01";
break;
case 1:
var currmonth = "02";
break;
case 2:
var currmonth = "03";
break;
case 3:
var currmonth = "04";
break;
case 4:
var currmonth = "05";
break;
case 5:
var currmonth = "06";
break;
case 6:
var currmonth = "07";
break;
case 7:
var currmonth = "08";
break;
case 8:
var currmonth = "09";
break;
case 9:
var currmonth = "10";
break;
case 10:
var currmonth = "11";
break;
case 11:
var currmonth = "12";
break;
}
alert("Month (new): " + currmonth);
if (pf.PaymentMethod.value == "Select") {
alert("Payment Method is required.");
pf.PaymentMethod.focus();
return false;
}
if (pf.CreditCardNumber.value == "") {
alert("Credit Card Number is required.");
pf.CreditCardNumber.focus();
return false;
}
if (pf.CreditCardNumber.value != "") {
if (!IsCreditCard(pf.CreditCardNumber)){
alert("Credit Card Number is not valid.");
pf.CreditCardNumber.focus();
return false;
}
}
alert("Exp Yr: " + pf.ExpirationYear.value);
if (pf.ExpirationYear.value < year) {
alert("Expiration Year must be equal to or greater than current year.");
pf.ExpirationYear.focus();
return false;
}
alert("Exp Month: " + pf.ExpirationMonth.value);
if (pf.ExpirationYear.value == year) {
if (pf.ExpirationMonth.value < currmonth) {
alert("Expiration Month must be equal to or greater than current month.");
pf.ExpirationMonth.focus();
return false;
}
}
if (pf.FirstName.value == "") {
alert("First Name as it appears on the card is required.");
pf.FirstName.focus();
return false;
}
if (pf.LastName.value == "") {
alert("Last Name as it appears on the card is required.");
pf.LastName.focus();
return false;
}
if (pf.CardID.value == "") {
alert("Card ID is required. Refer to the help icon for assistance.");
pf.CardID.focus();
return false;
}
}
function IsCreditCard (s) {
//alert("Is it a valid Credit Card?");
validChars = "1234567890";
var st = "";
sum = 0;
mul = 1;
if (s){
s = s.value ;
l = s.length;
// strip all characters that are not valid numbers
for (i = 0; i < l; i++) {
var c = s.charAt(i);
if (validChars.indexOf(c) != -1) {
st += c;
}
}
//alert("Stripped credit card number :" + st);
if (st.length == 0) {
return false;
} else {
mul = 1;
l = st.length;
// validate credit card number using Luhn Mod-10 test
for (i = 0; i < l; i++) {
digit = st.substring(l-i-1,l-i);
tproduct = parseInt(digit ,10)*mul;
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
mul++;
else
mul--;
}
if ((sum % 10) == 0) {
return true;
} else {
return false;
}
}
} else {
return true;
}
}