11

I have an input mask on my text box like

        000,000,000.00

I have the following jQuery code to check the value of text box before submitting data.

    var txt_box = $('#txt_box').attr('value');

     if(txt_box <= 0 )

But now even if I didn't input any data, the box remains empty with only the input mask, data is submitting.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
air
  • 6,136
  • 26
  • 93
  • 125

2 Answers2

13

First: Separating floating points with a dot notation is required.

Second: You are checking for equal or less not just less than.

Third: Use parseInt() or parseFloat() to convert that input string into a Number.

Example:

var txt_box = $('#txt_box').attr('value').replace(/,/g, ".");
    if(parseFloat(txt_box) <= 0 )
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jAndy
  • 231,737
  • 57
  • 305
  • 359
3

You use <= instead < Anyway, you should parse value to number too.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66