-1

I just came across a strange situation while comparing two integer values. One value is read from Database and its type is int so this is an int value. Second value comes from a textbox whose type is number.

If textbox value is less than 100 than comparison is fine if it is greater than 100 comparison shows strange results.

If i use parseInt than everything is fine. I just want to know why this is happening ?

var CADMeterReading = 10;
<input type="number" name="MeterReading" placeholder="0000" value="20" id="MeterReading"   />

if($("#MeterReading").val()) <= CADMeterReading) //returns false as excepted

If textbox has value greater than 100 than it is not comparing as expected

var CADMeterReading = 80;
<input type="number" name="MeterReading" placeholder="0000" value="100" id="MeterReading"   />

if($("#MeterReading").val()) <= CADMeterReading) //returns true 
Mairaj Ahmad
  • 13,670
  • 2
  • 24
  • 40

3 Answers3

4

Defining the type of the input as number only stops the use from entering anything else than numbers:

<input type="number" name="MeterReading" placeholder="0000" value="20" id="MeterReading"/>

But that does not means that javascript will read it as a number.

$("#MeterReading").val() will return the data as String type, and hence you are comparing a string and an integer, which results in strange comparison.

That is also why when you use parseInt, the numbers are compared properly, since you have now asked javascript to convert the string to integer before comparing.

Ahs N
  • 7,860
  • 1
  • 23
  • 31
  • But why this works fine for values less than 100 ? – Mairaj Ahmad Nov 27 '15 at 07:24
  • That is probably because the ASCII value of the _character_ you entered is less than the _integer_ you are comparing it to, which results in giving you the 'seemingly' correct comparison result. – Ahs N Nov 27 '15 at 07:27
  • Hi Its still working without parseInt. Mairaj in your code you kept an extra ')' in condition. may be that would be an issue.. **Demo link :** http://jsfiddle.net/asimshahiddIT/8s3vd59r – asimshahiddIT Nov 27 '15 at 07:33
  • Actually, the comparison will work as-is without `parseInt` (etc) as long as one operand is not-a-string. – Salman A Nov 27 '15 at 07:36
1

An input element will always return a string, this is just how jQuery.val() works, and I think to a large degree takes it's lead from the HTML spec (which defines the return value of inputElement.value as type DOMString).

See more discussion of this here: How can val() return Number? , but essentially it sounds like you've found the answer already in terms of functionality - use parseInt($("#MeterReading").val(),10)

Community
  • 1
  • 1
Chris O'Kelly
  • 1,733
  • 2
  • 16
  • 34
0

Defining number, doesn't mean that it provides value in integer, it always gives you as string So you need to use parseInt() to convert it into integer for compare number

Its string comparison, try "100" < 101, this give you true So don't take it like it not works with above 100

chirag.sweng
  • 471
  • 1
  • 10
  • 24