Click to See Complete Forum and Search --> : validate sum of a few textboxes?


sanjuT
05-05-2003, 09:38 AM
i was wondering if it's possible to validate the sum of a few textboxes..

i have a group of about 6 textboxes where the user enters a number for quantity. there are about 4 such groups.

within each group of 6, the total selected by the user has to be at least 5.

i.e. 2 firstitem, 2 seconditem, 1 fithitem (can be more than 5 total).

this is what i have so far, but it only validates one textbox:

//Validate min order
if ((document.receptionitems.qty1.value!="") && (document.receptionitems.qty1.value<5))
{
window.alert("\n" + "Cold Canapés (Premium) require a minimum order of 5");
document.receptionitems.qty1.focus();
return false;
}

can i tweak this code to check for the total of 6 different textboxes? Do i need to use arrays?

THANKS!!!

Jona
05-05-2003, 11:00 AM
Perhaps you're looking for a for() loop:

//Validate min order
for(x=0;x<=24;x++){
if ((document.receptionitems.qty[x].value!="")&&(document.receptionitems.qty[x].value<5)){
window.alert("\n" + "Cold Canapés (Premium) require a minimum order of 5");
document.receptionitems.qty[x].focus();
return false;
}


Something like that?

sanjuT
05-05-2003, 11:08 AM
a couple of things:

i) i get this in the netscape debugger:
document.receptionitems.qty has no properties.

ii) i do not want to check all of the textboxes, just the ones from document.receptionitems.qty1.value to document.receptionitems.qty5.value.

I have about 31 total textboxes.

Jona
05-05-2003, 11:16 AM
//Validate min order
for(x=0;x<=5;x++){
if ((document.receptionitems.qty[x].value!="")&&(document.receptionitems.qty[x].value<5)){
window.alert("\n" + "Cold Canapés (Premium) require a minimum order of 5");
return false;
}

As for Netscape's problem, try using Internet Explorer for testing purposes... ;) In any case, the code above looks to me like it should work.

sanjuT
05-05-2003, 11:28 AM
i tested in IE, all it did was clear the textboxes and put the cursor in the 1st field.

i need it to work in Netscape though, it's on a company intranet and our standard is NS 4.76 (i know, that's stupid, but my boss won't budge)..

Jona
05-05-2003, 11:40 AM
Oh! You mean NN4! Haha, well, in that case... We'll have to work a little harder. ;)

//Validate min order
for(x=0;x<=5;x++){
var qtyBox = eval("document.receptionitems.qty["+x+"].value");
if((qtyBox!="")&&(qtyBox.value<5)){
window.alert("\n" + "Cold Canapés (Premium) require a minimum order of 5");
return false;
}

I hope that works..... :rolleyes:

sanjuT
05-05-2003, 11:58 AM
thanks for all your help, but i still get this error:

document.receptionitems.qty has no properties.

I don't know why, i use that part elsewhere in the page and it works..
here's my entire validation script:

function send() {

// Outer loop that cycles through every element
// of the form
var empty_fields;
var msg;
var empty_fields = "";


for(x=0;x<=5;x++){
var qtyBox = eval("document.receptionitems.qty["+x+"].value");
if((qtyBox!="")&&(qtyBox.value<5)){
window.alert("\n" + "(Premium) require a minimum order of 5");
return false;
}



var mess;
var newitem;
mess="Your current order for the first break is:\n";

if (document.receptionitems.qty1.value!="") {
newitem = "\n" + document.receptionitems.qty1.value + " Cold Canapés (Premium)";
}

if (document.receptionitems.qty2.value!="") {
newitem += "\n" + document.receptionitems.qty2.value + " Cold Canapés (Gourmet)";
}

if (document.receptionitems.qty3.value!="") {
newitem += "\n" + document.receptionitems.qty3.value + " Hot Hors D'oeuvres (Premium)";
}
if (document.receptionitems.qty4.value!="") {
newitem += "\n" + document.receptionitems.qty4.value + " Hot Hors D'oeuvres (Gourmet)";
}
if (document.receptionitems.qty5.value!="") {
newitem += "\n" + document.receptionitems.qty5.value + " Root Vegetables & Florets w/Yogurt Garlic & Cucumber Dip";
}

if (document.receptionitems.qty6.value!="") {
newitem += "\n" + document.receptionitems.qty6.value + " Assortment of Cheeses garnished with Grapes and Biscuits";
}
if (document.receptionitems.qty7.value!="") {
newitem += "\n" + document.receptionitems.qty7.value + " Seasonal Fresh Fruit & Berries w/ Sweet Yogurt Condiment";
}
if (document.receptionitems.qty8.value!="") {
newitem += "\n" + document.receptionitems.qty8.value + " Chocolate Truffles and Strawberries";
}
if (document.receptionitems.qty9.value!="") {
newitem += "\n" + document.receptionitems.qty9.value + " Assorted French Pastries";
}
if (document.receptionitems.qty10.value!="") {
newitem += "\n" + document.receptionitems.qty10.value + " Assorted ****tail Sandwiches";
}
if (document.receptionitems.qty11.value!="") {
newitem += "\n" + document.receptionitems.qty11.value + " Deli Style Sandwiches";
}
if (document.receptionitems.qty12.value!="") {
newitem += "\n" + document.receptionitems.qty12.value + " Gourmet Open Faced Sandwiches";
}
if (document.receptionitems.qty13.value!="") {
newitem += "\n" + document.receptionitems.qty13.value + " Hot Roast Sirloin of Beef Carved in Room with Mini Kaiser buns";
}
if (document.receptionitems.qty14.value!="") {
newitem += "\n" + document.receptionitems.qty14.value + " Carved Hip of Beef Whole Hip of Beef Carved in room";
}

if (confirm(mess + newitem) && (newitem!="")) {
window.opener.document.Layer11.document.form1.ReceptionItems.value=newitem;
window.close();
}
else return false;
}
}

THANKS!!!!!!

Jona
05-05-2003, 12:04 PM
Okay, we're starting an array in a for() loop, which means we start at zero. We need to start at one.

Replace the for() loop with this:
for(x=1;x<=5;x++)

sanjuT
05-05-2003, 12:23 PM
i'm sorry, but i still get that stupid error about no properties...

would u have any idea why i get that?

thanks for all the help!!!!!

Jona
05-05-2003, 12:42 PM
I don't work with NN4 often, so I'm not really sure. I know what the error means. It means that the object, "document.forms[0].qty[x]" exists, but has no properties. So, for some reason, it's clearing the properties of qty[x] (where x is the variable in our for() loop). I'm not exactly sure why or how, but it seems like that's what it's doing. I don't have NN4 (and I can't download it yet), so I can't exactly help you debug. Sorry.

sanjuT
05-05-2003, 01:46 PM
hey, don't worry about it, u've been spending a lot of time trying to help me.
i appreciate that, thanks!!!

sanjuT
05-05-2003, 03:41 PM
i have this so far:

var ham = document.receptionitems.qty1.value;
var cheese = document.receptionitems.qty2.value;
var chicken = document.receptionitems.qty3.value;
var shrimp = document.receptionitems.qty4.value;
var egg = document.receptionitems.qty5.value;
var total = parseInt(ham) + parseInt(cheese) + parseInt(chicken) + parseInt(shrimp) + parseInt(egg);
window.alert(total)
if ((total!="") && (total<5))
{
window.alert("works!")
document.receptionitems.qty1.focus();
return false;
}

Problem: i get 'NaN' in the alert box if any of these fields are left blank. but the user should be able to leave any or all of these fields blank..

any ideas anyone???
THANKS!

Jona
05-05-2003, 04:24 PM
if(NaN(ham)){document.receptionitems.qty1.value="0";}

Use for all of them: ham, cheese, chicken, shrimp, etc.

sanjuT
05-06-2003, 08:38 AM
i get the error:

NaN is not a function

Jona
05-06-2003, 10:26 PM
Dummy me, I forgot, you use if(isNaN(ham)) :p

sanjuT
05-07-2003, 08:18 AM
THANKS!!
This is what I have, and it works:

var total = 0;

var ham = parseInt(document.receptionitems.qty1.value);
if( !isNaN( ham ) ) total += ham;

var cheese = parseInt(document.receptionitems.qty2.value);
if( !isNaN( cheese ) ) total += cheese;

var chicken = parseInt(document.receptionitems.qty3.value);
if( !isNaN( chicken ) ) total += chicken;

ect....

if ((total!="") && (total<5))
{
window.alert("You must selected a minimum total of 5 Gourmet Cold Canapes")
document.receptionitems.qty1.focus();
return false;
}

THANKS AGAIN!

Jona
05-07-2003, 10:57 AM
No problem! ;)