This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes validate and add 5 textfields... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "validate and add 5 textfields..." Watch "validate and add 5 textfields..." New topic
Author

validate and add 5 textfields...

joe script
Greenhorn

Joined: May 05, 2003
Posts: 11
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: May 05, 2003
Posts: 11
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
you can do

if(document.FormName.ElementName.value)addvalue++;
else addvalue=0;
joe script
Greenhorn

Joined: May 05, 2003
Posts: 11
i get the error:
NaN
not sure what to do..
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Hey Joe,
isNaN( value ) returns true if value is not a number. e.g.
joe script
Greenhorn

Joined: May 05, 2003
Posts: 11
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

Joined: Mar 21, 2003
Posts: 1049
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

Joined: May 05, 2003
Posts: 11
thanks a lot!!!
it works now! (using total = 0
seems pretty easy now, but i was real lost..
THANKS ALL!!
 
Don't get me started about those stupid light bulbs.
 
subject: validate and add 5 textfields...
 
Similar Threads
Checking user selection
What came first - the chicken or the egg?
RequestDispatcher Include
Can have Checkbox & Textfield together ????
search functionality with 6 inputs(oracle based)