Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Calculation not working 1

Status
Not open for further replies.

ecobb

Programmer
Dec 5, 2002
2,190
US
I know next to nothing about JavaScript, and I'm having problems getting a simple calculation to work. Here's what I'm trying to do. I have a signup form where users can order tickets to an event. There are only 25 seats available, and I'm keeping track of how many tickets have already been sold. I don't want the user to be able to order more tickets that we have left. I dynamically populate my JavaScript with the number of remaining tickets, and I want to check my form box on submission.

Here's my code, the form validation already works, I just can't get the calculation to work right.
Code:
<script type="text/javascript">
<!--
function validate_form ( )
{
	valid = true;
        if ( document.registration_form.quantity.value == "" )
        {
			alert ( "You must enter the Number of Tickets you want." );
			valid = false;
        }
        [blue]if ( document.registration_form.quantity.value + 20 > "25" )
        {
			alert ( "There are only 5 spaces left." );
			valid = false;
        }[/blue]
        if ( document.registration_form.os0.value == "" )
        {
			alert ( "You must provide your Name." );
			valid = false;
        }
        return valid;
}
//-->
</script>
Basically, if the number of tickets requested + the number of available tickets is GT 25, I want the alert to pop up. It works correctly if I try to order 1 or 2 tickets, but any other quantity throws the alert box.

Any ideas as to what could be wrong?


Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 
if ( document.registration_form.quantity.value + 20 > [!]"25"[/!] )

Numbers do not use quotations.

[monkey][snake] <.
 
Hmm, see this:

Code:
if ( document.registration_form.quantity.value + 20 > "25" )

Why not just compare the value > 5

Code:
if (document.registration_form.quantity.value > 5)


[monkey][snake] <.
 
Thanks, I removed the quotes but unfortunately I get the same results. Any numbers other than "1" or "2" entered into the form trigger the alert box.


Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 
Ok, then do this at the top of your validateForm function:

Code:
var quantity = [!]parseInt([/!]document.registration_form.quantity.value[!], 10)[/!]

Substitute all instances of
document.registration_form.quantity.value with
quantity.

[monkey][snake] <.
 
Worked like a charm! I guess I just over-complicated things. Thanks monksnake!

Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 
Thanks for that last tip, too. I'll add it in as well.


Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top