Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validate and add 5 textfields...

 
joe script
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a group of 5 textfields, the user enters a quantity in them.
I need:
- if the user enters a quantity in any of the textboxes, the total for the entire group must be at least 5
- if the user doesn't enter any values in the textboxes, then the form will process as normal (the user does not have to enter any quantity in these fields.)
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;
}

so the 2 problems i mentioned at the start were what was bugging me. when there is a blank textbox, the alert i get just says: NaN
If anyone can help, i'd greatly appreciate it!!
THANKS.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and welcome to the ranch....
First off your name does not apply to the naming standards at the ranch. Please change your display name by clicking on the my profile link above.
Second off an easy fix is just automatically place a 9 into the text boxes as the default value.
Eric
 
joe script
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'll change my name in a sec...thx for the warm welcome.
i assume u meant place a 0 in the textboxes..but this would create a whole bunch of problems for me with my other scripts, and with the way my servlet processes the form.
is there a fix that actually deals with tweaking the code?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do

if(document.FormName.ElementName.value)addvalue++;
else addvalue=0;
 
joe script
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get the error:
NaN
not sure what to do..
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Joe,
isNaN( value ) returns true if value is not a number. e.g.
 
joe script
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i am using:
var total = "";
var ham = parseInt(document.receptionitems.qty1.value);
if( !isNaN( ham ) ) total += ham;
var cheese = parseInt(document.receptionitems.qty2.value);
if( !isNaN( cheese ) ) total += cheese;
etc.....
window.alert(total)
if ((total!="") && (total<5))
{
window.alert("works!")
document.receptionitems.qty1.focus();
return false;
}
BUT i get the values the user has entered, not the SUM of the values..
I need one number, the sum of the values (eg. 1+2+1+2 = 6, so 6 is what i need).
Any ideas?
THANKS!!!
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
first try: var total = 0; // because "" is a String
if this doesn'work try:

instead of total += ham
!but write e + val as 1 word. ( The javaranch-sofware does'nt accept it as 1 )!
hth cb
[ May 06, 2003: Message edited by: Christian Baron ]
 
joe script
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot!!!
it works now! (using total = 0
seems pretty easy now, but i was real lost..
THANKS ALL!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic