rjcode
rjcode

Reputation: 1349

jquery if else compare query issue

I have below jquery if\else comparison to evaluate part of code based on results,

if($('#order_quantity').val() < $('#available_quantity').val()) {
   // ---code to run----
} else {
   // --- code to show error---
}

This code work 3-4 times correctly for inputs and after that it working wrong,

when $('#order_quantity').val() = 5 and $('#available_quantity').val() = 46, then ideally --code to run-- should execute but it is running --code to show error -- of else statement.

Sometimes it correct and sometimes it not, I created console.log for above input values, inputs are correct but still comparison results throw wrong results

What can be problem in this comparison?

Upvotes: 1

Views: 706

Answers (5)

Tushar
Tushar

Reputation: 87203

The values read from the DOM are strings. You need to convert it to numbers in order to use it in arithmetic operation unless the operator coerces the operand to number.

if(+$('#order_quantity').val() < +$('#available_quantity').val())

You can convert string to number using

  1. By preceding the string with unary + operator, this will coerce the string to Number.
  2. By using Number function
  3. Using parseFloat
  4. Using parseInt, but this will convert the float value to integer.

this code work 3-4 times correctly for inputs and after that it working wrong

The code works for some values because how the strings are compared.

> "5" < "46" // false

The strings are compared character by character just like how sorting works. In this code the comparison is done as '5' < '4', when comparing the strings their ASCII values is compared. As the ASCII value of 5 is greater than that of 4 the comparison returns false.

Upvotes: 6

ryubro
ryubro

Reputation: 338

The type of $('#order_quantity').val() is string. '5' > '46' is true just like 'b' > 'a' is true. To convert string value to int value, you can use parseInt function. To get the intended result your code should be something like:

if (parseInt($('#order_quantity').val()) < parseInt($('#available_quantity').val()))
  {
    //---code to run----
  }
else
  {
    //--- code to show error---
  }

Upvotes: 2

Webloper
Webloper

Reputation: 178

Try these solution :

var order_quantity =  $('#order_quantity').val();
var available_quantity = $('#available_quantity').val();

if( Number( order_quantity ) < Number( available_quantity ) )
{ //---code to run---- }
else
{ //--- code to show error---}

Number

Or

if (order_quantity - available_quantity  < 0)
{ //---code to run---- }
else
{ //--- code to show error---}

This forces javascript to process a numeric calculation. Strings will be converted into numbers.

Upvotes: 2

Ionică Bizău
Ionică Bizău

Reputation: 113375

val() returns a string. You have to convert these string values in numbers:

if(Number($('#order_quantity').val()) < Number($('#available_quantity').val())) {
     // do something
} else {
     // do something
}

An example when your expression fails:

"24" < "124"
false

There are other ways to convert strings into numbers (see What's the fastest way to convert String to Number in JavaScript?):

  • Number(x)
  • parseInt(x, 10) (for integers)
  • parseFloat(x)
  • +x;
  • ~~x (for integers)
  • x | 0 (for integers)

...or using Math methods. x is the string value.

Upvotes: 5

Neha
Neha

Reputation: 673

You will need to convert string to integer as all values read with jQuery val() method are returned as strings.

You can also use this code -

if(parseInt($('#order_quantity').val()) < parseInt($('#available_quantity').val()))

Upvotes: 2

Related Questions