Form Validation before submit not working

I’ll start out by confessing that I only dabble in javascript. But I need it in this case to validate information before allowing the item to be added to the PayPal cart.

Here is the Javascript:

function validateFields() { 

    var message = " ";
    var errors = "n";
    var strandType = document.PayPal.fullOrHalf.value;
    
        var taxExempt = document.PayPal.taxExempt.value;
        var qty = document.PayPal.quantity.value;
        var maxQty = document.PayPal.maxQuantity.value;
    
    if ( isLoggedIn(taxExempt) ) { 
    } else { 
        message += "\
 - You must be logged in before you can make a purchase. Login button is at the top of the page."; 
        errors = "y";
    }

    if (isNumber(qty)) { 
        if ( qtyInRange(qty,maxQty ) ) {
        } else { 
            if ( strandType == "full" ) { 
                message += "\
 - Quantity must be between 1 and " + maxQty + ".\
";
                errors = "y";
            } else { 
                message += "\
 - Quantity for half strand must be between 1 and " + maxQty + ".\
";
                errors = "y";
            }
        }
    } else {
        if ( strandType == "full" ) { 
            message += "\
 - Quantity must be a number. \
";
            errors = "y";
        } else { 
            message += "\
 - Quantity for half strand must be a number.\
";
            errors = "y";
        }
    }

    if ( errors == "n";) { 
        /* Continue with submission */
        return true;
    } else { 
        alert("Errors found" + message); 
        /* Abort submission */
        return false;
    }
    return true;
}    // end fx validateFields

function qtyInRange(orderQty, maxQty) { 

    if ( ( orderQty > 0 ) && ( orderQty <= maxQty ) ) { 
    } else { 
        return false;
    }
    return true;
}    // end fx qtyInRange

function isNumber(str) { return !isNaN(Number(str)); }

function isLoggedIn(status) { 
    if ( status == "?" ) { 
        return false;
    }
    return true;
}    // end fx isLoggedIn


And here is the form:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="PayPal" target="paypal" id="PayPal">
  <input name="maxQuantity" type="hidden" id="maxQuantity" value="0" />
  <input type="hidden" name="fullOrHalf"   value="full" />
  <input type="text" name="quantity"   value="1" size="3" maxlength="5" />
  <input type="hidden" name="cmd" value="_cart" />
  <input type="hidden" name="add" value="1" />
  <input type="hidden" name="business" value="adrianasbeads@yahoo.com" />
  <input type="hidden" name="item_name" value="cherry necklace - matches bracelet BB064" />
  <input type="hidden" name="item_number" value="BN005" />
  <input type="hidden" name="amount" value="147.50" />
  <input type="hidden" name="tax" value="0.00">  <input type="hidden" name="currency_code" value="USD" />
  <input type="hidden" name="return" value="http://adrianasbeads.com/thankyou.php" />
  <input type="hidden" name="cancel_return" value="http://adrianasbeads.com/PaymentCancel.php" />
  <input type="hidden" name="receiver_email" value="adrianasbeads@yahoo.com" />
  <input type="hidden" name="no_shipping" value="2" />
  <input type="hidden" name="no_note" value="0" />
  <input type="hidden" name="notify_url" value="http://adrianasbeads.com/ABeadsIPN.php" />  <input type="hidden" name="taxExempt" size="1" value="Y" />
  <input name="submit" type="image"  src="images/addToCart.png" alt="Click button to add item to cart." />
</form>


I’ve been working on this for days with no success. No matter what I do, the form is submitted. Any ideas?

I’m not sure what is going on. I even set validateFields() to just return false (see below). And it still submits. There’s got to be something I’m doing wrong.

Any ideas?

function validateFields() {
 
    return false;
}    // end fx validateFields

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="PayPal" target="paypal" id="PayPal" onsubmit="validateFields()" >
  <input name="maxQuantity" type="hidden" id="maxQuantity" value="1" />
  <input type="hidden" name="fullOrHalf"   value="full" />
  <input type="text" name="quantity"   value="1" size="3" maxlength="5" />
  <input type="hidden" name="cmd" value="_cart" />
  <input type="hidden" name="add" value="1" />
  <input type="hidden" name="business" value="adrianasbeads@yahoo.com" />
  <input type="hidden" name="item_name" value="bracelet - 0" />
  <input type="hidden" name="item_number" value="BB044" />
  <input type="hidden" name="amount" value="87.50" />
    <input type="hidden" name="currency_code" value="USD" />
  <input type="hidden" name="return" value="http://adrianasbeads.com/thankyou.php" />
  <input type="hidden" name="cancel_return" value="http://adrianasbeads.com/PaymentCancel.php" />
  <input type="hidden" name="receiver_email" value="adrianasbeads@yahoo.com" />
  <input type="hidden" name="no_shipping" value="2" />
  <input type="hidden" name="no_note" value="0" />
  <input type="hidden" name="notify_url" value="http://adrianasbeads.com/ABeadsIPN.php" />  <input type="hidden" name="taxExempt" size="1" value="?" />
  <input name="submit" type="image"  src="images/addToCart.png" alt="Click button to add item to cart." />
</form>

Thanks for the quick answer, SgtLegend. Unfortunately, it didn’t seem to make a difference. Any other ideas?

Add

onclick="validateFields()"

to your submit input element

[COLOR=#005500][COLOR=black]

 [/COLOR]
[COLOR=black][COLOR=#005500]onsubmit[/COLOR]=[COLOR=#cc0000]"validateFields()"[/COLOR][/COLOR]
[COLOR=black]

is incorrect.[/COLOR]

You need

[COLOR=#005500]

 
[COLOR=#005500]onsubmit[/COLOR]=[COLOR=#cc0000]"return validateFields()"[/COLOR]

[/COLOR][/COLOR]

You need something like this.

 
<form action="some_url" method="post" onsubmit="return validateForm();" />

validateForm() must return a value of true or false depending on whether all the form inputs are valid or not.

If it returns true, then the form will be submitted. If it returns false, the form is not submitted.