Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate before Submitting

Status
Not open for further replies.

jillallynwilson

Programmer
Apr 14, 2004
21
US
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;
}
}
 
age-old question.

don't call the function from the submit button's onclick event. instead, call it from the form's onsubmit event.

Code:
<form name="_Payment" onsubmit="return ValidateCreditCard(this);">

and, incidentally, you can remove that unnecessary if statement:

Code:
  [s]if (pf == "Payment") {
    var pf = document._Payment;  
  }[/s]

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Thank you for such I quick reply. I have added this to my form tag, however, what goes in the submit button code then?

NOTE: I am not using the standard HTML Submit button

<a href="javascript: SubmitForm()" onmouseover="window.status='Complete Order';return true"><img src="CompleteOrder.gif" border="0"></a>
 
you might try two things:

1)

Code:
<a href="#" onclick="if ( ValidateCreditCard(document._Payment) ) { document._Payment.submit(); return false; }" onmouseover="window.status='Complete Order';">

or

2) style a real input button to LOOK LIKE an anchor tag.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Nevermind my previous post. I was:

javascript: onclick=Submit();

However, the form still submitted even through it did not pass the validation check.

Any other ideas?
 
any other ideas? you haven't even tried the ones i've given you.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Sorry I missed you previous post. We must have been posting a reply at the same time.

Let me try your other suggestion.

Thank you.
 
Perhaps your code is failing due to JavaScript errors made by this line:

Code:
s = s.value;

The line is not needed, and breaks as string variables have no value property.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
The following code finally worked:

<a href="#" onclick="if (ValidateCreditCard(document._Payment)) { document._Payment.submit();} else { return false; }" onmouseover="window.status='Complete Order';">

Thank you for your help :eek:)

Happy Thanksgiving!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top