Fw: JavaScript - Validate a form

by "Sara Winters" <saraw(at)fcs.net>

 Date:  Wed, 16 Aug 2000 10:43:56 -0700
 To:  "hwg languages" <hwg-languages(at)hwg.org>
  todo: View Thread, Original
Thank you to everyone for their suggestions.  I really appreciated it.
My coworker was able to get the validate credit card function to work by
moving the function call
"validateCreditCardNumber()" into another function called checkForm(), and
then
calling only the checkForm() function alone in the submit button (the
validateCreditCardNumber
function is not included here, but in my previous email).

Here is the checkForm() function with the validateCreditCardNumber()
function embedded in it"

function checkForm() {

 if (document.forms[ 0 ].subtotal.value == 0) {
  alert("You have not selected any wine to purchase.");
  return;
 }

 setTotals();

 if ( checkLength(document.forms[ 0 ].namef.value.length) == false ) {
  alert("A First Name is required in the Billing section.");
  return;
 }
 if ( checkLength(document.forms[ 0 ].namel.value.length) == false ) {
  alert("A Last Name is required in the Billing section.");
  return;
 }
 if ( checkLength(document.forms[ 0 ].address.value.length) == false) {
  alert("An Address is required in the Billing section.");
  return;
 }
 if ( checkLength(document.forms[ 0 ].city.value.length) == false ) {
  alert("A City is required in the Billing section.");
  return;
 }
 if (countryOK1() == false) {
  return;
 }
 if (zipOK(document.forms[ 0 ].zip.value,"Billing") == false) {
  return;
 }
 if ( emailValid(document.forms[ 0 ].email.value,"Billing") == false ) {
  return;
 }

 // Validate shipping info.
 if ( checkLength(document.forms[ 0 ].shipnamef.value.length) == false ) {
  alert("A First Name is required in the Shipping section.");
  return;
 }
 if ( checkLength(document.forms[ 0 ].shipnamel.value.length) == false ) {
  alert("A Last Name is required in the Shipping section.");
  return;
 }
    if ( checkLength(document.forms[ 0 ].shipaddress.value.length) == false)
{
  alert("An Address is required in the Shipping section.");
  return;
 }
 if ( checkLength(document.forms[ 0 ].shipcity.value.length) == false ) {
  alert("A City is required in the Shipping section.");
  return;
 }
 if (countryOK2() == false) {
  return;
 }
 if (zipOK(document.forms[ 0 ].shipzip.value,"Shipping") == false) {
  return;
 }

 if ( validateCreditCardNumber() == false ) {
  return;
 }

    document.forms[ 0 ].submit();
}


Here is the submit button calling only the checkForm() function:

<INPUT TYPE="submit" VALUE=" Continue Your Order " onClick="checkForm();
return false;">

Thanks again for the suggestions!

Sara


----- Original Message -----
From: "Sara Winters" <saraw(at)fcs.net>
To: "hwg languages" <hwg-languages(at)hwg.org>
Sent: Monday, August 14, 2000 2:39 PM
Subject: JavaScript - Validate a form


> Hi Everyone,
>
> I am having trouble validating a form.  It is a shopping cart page, and
all
> of the the other fields (such as billing and shipping address) are working
> fine, and alert the user if they forget to add a piece of information.
> However, when the user adds their credit card information, the validation
is
> not working properly (it is the last step on the page).
> An alert box does appear if the user forgets a piece of credit card info
> when the user clicks the submit button.  But instead of returning false,
and
> allowing the user to enter the credit card information, the order is
> submitted, and the user is taken to the next which displays their receipt.
>
> The function to validate the credit card information is called next to the
> "submit" button, and I am wondering if I should call this function earlier
> in the form so the order will not submit until all of the info is entered.
> Below are my functions and the submit button - any help would be greatly
> appreciated.
>
> Thank you!
>
> Sara
>
>
>
> function isGoodYear(y)
>     {
>
>   // Use this if you do not have access to the servers system date.
>   // Get current year from client machine
>   // myDate = new Date();
>   // myYear = (myDate.getYear() + 1900);
>   // myMonth = (myDate.getMonth()); // Remember that JavaScript getMonth
> will return a number between 00 and 11
>   // alert(myYear);
>
>   // Get current year from server
>   myYear = (:sYear);
>   myMonth = (:sMonth);
>   theirYear =
>
(document.forms[ 0 ].ccYear.options[document.forms[ 0 ].ccYear.selectedIndex
> ].value);
>
>   // Find out if the year they gave is equal or greater or less than
current
> year
>     if (theirYear > myYear)
>         {
>         return true;
>         }
>   else if (myYear > theirYear)
>     {
>         alert("Your credit card seems to have expired (You entered " +
> theirYear + ". It is now " + myYear + ".)\nPlease enter a updated
expiration
> or try another credit card.");
>         return false;
>     }
>   else
>     {
>         // it is equal, check the month.
>     theirMonth =
>
(document.forms[ 0 ].ccMonth.options[document.forms[ 0 ].ccMonth.selectedInd
> ex].value);
>     if (myMonth > theirMonth)
>       {
>       alert("Your credit card seems to have expired\n(You entered " +
> theirMonth + ", " + theirYear + ". It is now " + myMonth + ", " + myYear +
> ".)\nPlease enter a updated expiration or try another credit card.");
>       return false;
>       }
>     }
>   return true;
>     }
>
>
>
> function validateCreditCardNumber()
>     {
>
>
>   // Next check that the proper amount of numbers have been entered for
the
> credit card.
>     if (document.forms[ 0 ].CreditCardNumber1.value.length != 4)
>         {
>         alert("The credit card number is not completely filled out");
>         return false;
>         }
>     if (document.forms[ 0 ].CreditCardNumber2.value.length != 4)
>         {
>         alert("The credit card number is not completely filled out");
>         return false;
>         }
>     if (document.forms[ 0 ].CreditCardNumber3.value.length != 4)
>         {
>         alert("The credit card number is not completely filled out");
>         return false;
>         }
>     if (document.forms[ 0 ].CreditCardNumber4.value.length != 4)
>         {
>         alert("The credit card number is not completely filled out");
>         return false;
>         }
>
>   // Next check that credit card entries were all numbers.
>     if (isAllDigits(document.forms[ 0 ].CreditCardNumber1.value) == false)
>         {
>         alert("The credit card number contains invalid characters");
>         return false;
>         }
>     if (isAllDigits(document.forms[ 0 ].CreditCardNumber2.value) == false)
>         {
>         alert("The credit card number contains invalid characters");
>         return false;
>         }
>     if (isAllDigits(document.forms[ 0 ].CreditCardNumber3.value) == false)
>         {
>         alert("The credit card number contains invalid characters");
>         return false;
>         }
>     if (isAllDigits(document.forms[ 0 ].CreditCardNumber4.value) == false)
>         {
>         alert("The credit card number contains invalid characters");
>         return false;
>         }
>
>
>
>   // Check the card's expiration date.
>     if (isGoodYear(document.forms[ 0 ].ccYear.value) == false)
>         {
>         return false;
>         }
>
>   // Next check that they filled out the Name-On-Card field.
>     if (4 > document.forms[ 0 ].ccName.value.length)
>         {
>     alert("The \"Name On Card\" field does not appear to be filled out
> properly.\nPlease enter a correct name and resubmit.");
>         return false;
>         }
>
>
>
>
>   // Removed Credit Card Type-Check from Radio Buttons from here.
>
>   // Check that the numbers pass the MOD10 test.
>     var sum = sumForMod10(document.forms[ 0 ].CreditCardNumber1.value);
>     sum += sumForMod10(document.forms[ 0 ].CreditCardNumber2.value);
>     sum += sumForMod10(document.forms[ 0 ].CreditCardNumber3.value);
>     sum += sumForMod10(document.forms[ 0 ].CreditCardNumber4.value);
>     if (sum % 10 != 0)
>         {
>         alert("The credit card number does not appear to be valid.\nPlease
> re-enter it.");
>         return false;
>         }
>
>   // Check the card's expiration date.
>     if (isGoodYear(document.forms[ 0 ].ccYear.value) == false)
>         {
>         return false;
>         }
>
>   // Check that they filled out the Name-On-Card field.
>     if (4 > document.forms[ 0 ].ccName.value.length)
>         {
>     alert("The \"Name On Card\" field does not appear to be filled out
> properly.\nPlease enter a correct name and resubmit.");
>         return false;
>         }
>     file://return true;
> }
>
>
> ------
>
> This my submit button calling the functions:
>
> <INPUT TYPE="submit" VALUE=" Continue Your Order " onClick="setTotals();
> checkForm(); validateCreditCardNumber(); return false;">
>
>

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA