Page 1 of 1

JavaScript .value question

Posted: Wed Dec 10, 2008 10:21 am
by icesolid
I have a simple if statement that determines if the number entered in a text input is greater than 150. It works as long as the number entered is 3 digits. However if someone enters 60 in the input box, which is less than 150, the if statement is true because it puts a 0 at the end and makes the 60 a 600. My code for my if statement is below:

Code: Select all

if(document.getElementById('process_form').charge.value > "150")
How can I fix this?

Re: JavaScript .value question

Posted: Wed Dec 10, 2008 10:59 am
by mintedjo
I dont think you should be using "150"
putting quotes around the number makes it a string object and comparing strings with numbers is generally not a good idea.

Also, how do you know that its adding a "0" to the end and making it 600?

Re: JavaScript .value question

Posted: Wed Dec 10, 2008 12:19 pm
by kaszu

Code: Select all

if(parseFloat(document.getElementById('process_form').charge.value) > 150)
It works with 60 because character "6" value is greater than "1" (like "b" > "a"), that's why "60" becomes the winner :)

Re: JavaScript .value question

Posted: Wed Dec 10, 2008 2:24 pm
by pickle
Change "150" to 150, then run the value of the field through parseInt().

Something's acting funky if it appends 0 to the value - that's not supposed to happen.