/    Sign up×
Community /Pin to ProfileBookmark

comparing numeric values

I have a form which runs some javascript code upon submission to check the validity of the data.

In one instance, I have a min, target and max value. min should always be less than target and target should always be less than max. These values are inputted into a text box.

I used the following if statement to catch any logical inconsistencies:

[CODE]if ((document.tForm.minH.value >= document.tForm.targetH.value) || (document.tForm.targetH.value >= document.tForm.maxH.value))
{
}[/CODE]

I believe the issue I’m having is that these values are being compared alpha-numerically, not numerically. If I type in 1, 2 and 3 respectively, it will pass. However, if I type in 1, 2 and 100000, it will not, because 100000 is being labeled as less than 2.

Do I have to convert these values to numbers (if so, how do you do that?), or is there a way to compare them as numbers? Or am I entirely missing the issue?

Thanks for any help.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 14.2006 — yes, attributes values return strings, not numbers. To have a bitwise math comparision you should tranform them first into numbers. One way is Number() method

..

var f = document.tForm;

if ((Number(f.minH.value) >= Number(f.targetH.value)) || (Number(f.targetH.value) >= Number(f.maxH.value)))
Copy linkTweet thisAlerts:
@tsp120authorMar 14.2006 — Thanks Kor. After i posted my question I decided I could have actually done a little research on my own. ?

I found using parseInt() works perfectly fine. I will just use that unless there is an advantage to using Number() you suggested.

Thanks again
Copy linkTweet thisAlerts:
@KorMar 14.2006 — well, the difference is that, if the field in empty, parseInt() will return NaN while Number() will return 0. The Number() could be thus useful in case of dynamical calculations.

parseInt() was designed mainly for other purposes such as to translate numbers from another base to decimal. It is also useful to keep and transform into decimal numbers strings like '25px', '60em', and things like that... and parseInt will return only the integer part of your number, as the name says...
Copy linkTweet thisAlerts:
@KorMar 14.2006 — If you think Number() is too long, you may also multyply or divide the string with/by 1

[I]string[/I]*1

[I]string[/I]/1
×

Success!

Help @tsp120 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...