By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

Need help with greater than if statement

P: 18
I'm trying to write an if statement to see if the answer I just entered is larger than the answer on the previous page.
But let's say the 1st number is 50 and the 2nd is 9. The alert comes up because the "9" is bigger than the "5" in 50.
How can I make this work?

I have a hidden field that is bringing the answer from page 1 to page 2.
[HTML]
<input type="hidden" name="Q1" value="$Q1">[/HTML]

This is my if statement:

Expand|Select|Wrap|Line Numbers
  1. answerA = document.forms[0].Q1.value 
  2. answerB = document.forms[0].Q2.value 
  3.  
  4.  if (answerB > answerA)
  5.       {
  6.     alert("\"Please enter a smaller number.")
  7.     FormQ2.Q2.focus(); 
  8.     return false;
  9.     } 
Please help if you can. Thanks :)
Nov 20 '08 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,390
just parseInt() or parseFloat() the values before comparing them ...

kind regards
Nov 20 '08 #2

P: 18
If I put this . . .

Expand|Select|Wrap|Line Numbers
  1. parseInt (answerA)
  2. parseInt (answerB)
  3.  
  4.   if (answerB > answerA)
  5.       {
  6.     alert("\"Please enter a smaller number.")
  7.     FormQ2.Q2.focus(); 
  8.     return false;
  9.     }   
  10.  

. . . the error comes up for any number entered. Do I need to do something other than just put in parseInt()?
Nov 20 '08 #3

gits
Expert Mod 5K+
P: 5,390
the usage should be like the following:

Expand|Select|Wrap|Line Numbers
  1. answerA = parseInt(document.forms[0].Q1.value);
the functioncall doesn't change the value itself ... you have to assign the value back to a variable like in the given example ...

kind regards
Nov 20 '08 #4

P: 18
Thanks for your help! I got it.
Dec 4 '08 #5

gits
Expert Mod 5K+
P: 5,390
no problem ... you're welcome. post back to the forums anytime you have more questions ;)

kind regards
Dec 5 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.