PDA

View Full Version : JavaScript to check Shopping Cart Form for Totals..


keenone
10-16-2002, 02:56 PM
I have a shopping cart with 5 items on one page. each item has a input field for qty. When the user hits "submit", i need the qty fields to be totaled, and if they are 5 or less, the form should not submit. I wrote this javascript:

<script language=javascript>
function cubes_Check(){
if(document.cubes.0003_quant.value + document.cubes.0004_quant.value + document.cubes.0005_quant.value + document.cubes.0006_quant.value + document.cubes.0007_quant.value < 6)
{
alert('Your Order Must Total 10 or More Cubes');
return false;
}
</script>

on the form tag I have this

OnSubmit='return cubes_Check();'

when i test the site, i get an error on line 5, which translates into the line where the "if" begins. It says I am missing a parentheses.

I am rather new to this and I can't find the problem. any help would be greatly appreciated.

Thanks.
Amy

beetle
10-16-2002, 03:13 PM
Change it up like this..function cubes_Check(){
var f = document.cubes;
var t = parsetInt(f.0003_quant.value) + parseInt(f.0004_quant.value) + parseInt(f.0005_quant.value) + parseInt(f.0006_quant.value) + parseInt(f.0007_quant.value);
if(t < 6) {
alert('Your Order Must Total 10 or More Cubes');
return false;
}
} <-- This sucker was missing

keenone
10-16-2002, 03:26 PM
thanks! didnt notice that. told ya i was new at this :)

I added that }. but,

I am still getting a "(" missing on line 5 error, then if i ignore that error, it complains about the piece of code in my form tag.

any other thoughts?

beetle
10-16-2002, 03:38 PM
maybe...

if(t < 6) {

to

if (t < 6) {

(missing space...)

keenone
10-16-2002, 03:46 PM
nope that didnt seem to do it..

but before i go any further,

thanks SOO MUCH for your help, you are a lifesaver and a kind person.

i am upping the page to:

www.millenianet.com/~webdesign/goodnews/cubes2.htm

perhaps seeing the whole thing can help us get thru this friggin problem :)

thanks again!

beetle
10-16-2002, 04:06 PM
Ok, know what the problem is.

The javascript chokes on these...

f.0003_quant.value

Why? Because the fieldname starts with a number, not a letter. A way around this is to use the elements collection

f.elements['0003_quant'].value

or to rename the elements to be more compatible

f.quant_0003.value

P.S. You really should put double quotes around ALL of your HTML attribute values....

keenone
10-16-2002, 04:16 PM
ah, i have had this problem before, i should have known.

thanks SO MUCH tho.

i now get thru with no errors.

the problem now- (it never ends)

is that if i type, say, 5 items in any of the blanks, it pops up with the correct error..

but if i put 5 in a blank and say, 2 in another, clearly less than ten, it lets me add the products.

sorry for the ongoing battle! but i DO appreicate this!

:)

keenone
10-16-2002, 04:18 PM
ps
i reupped the page to the same address with the changes u suggested.

thanks.

beetle
10-16-2002, 04:27 PM
I can easily use your original code to point out the problem here...

function cubes_Check(){
if(document.cubes.0003_quant.value + document.cubes.0004_quant.value + document.cubes.0005_quant.value + document.cubes.0006_quant.value + document.cubes.0007_quant.value < 6)
{
alert('Your Order Must Total 10 or More Cubes');
return false;
}

See it? hehe :D

keenone
10-16-2002, 04:32 PM
haha yeah i noticed that, but i fixed that part :)

what i think the problem is- is that my sum is adding everything as a string instead as a #.

what is the command to force a variable to be numeric?

beetle
10-16-2002, 04:33 PM
In my version of the function, I fixed that....here it is again...function cubes_Check(){
var f = document.cubes;
var t = parseInt(f.0003_quant.value) + parseInt(f.0004_quant.value) + parseInt(f.0005_quant.value) + parseInt(f.0006_quant.value) + parseInt(f.0007_quant.value);
if(t < 10) {
alert('Your Order Must Total 10 or More Cubes');
return false;
}
}

keenone
10-16-2002, 04:39 PM
beetle my hero... lovely...

one thing- its back to giving me that damn paren error again! i copied in just what u had there.. did i leave something out?


grr... :D

keenone
10-16-2002, 04:42 PM
ah! i realized its the same problem u just helped me with.. having f.005 etc be a #,
i changed them to f.c005 etc.

THANKS SOO MUCH!

if you ever need any front end help, flash, etc, i got you :)

www.subliminalorphans.com
Amy aka keen