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.
An alternative and more common approach is to use the less than or equals to sign. To check if a number is not greater than 0 , check if the number is less than or equal to 0 , e.g. num <= 0 . If the condition returns true , the number is not greater than 0 . Copied!
No zero is not less then zero, i <= 0 becomes 1 because zero is less than or equal to zero. Save this answer.
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 )
You use <=
instead <
Anyway, you should parse value
to number too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With