2

I try to check non negative number in jquery.If other then number my function works but for zero and non negative number its doesn't work.Here is my sample fiddle.
Sample Fiddle
Unable to find my mistake.Thanks.

4b0
  • 21,981
  • 30
  • 95
  • 142

4 Answers4

1

How about DEMO (NOTE: Error messages are OP's own)

$('#txtNumber').keyup(function() {
    var val = $(this).val(), error ="";
    $('#lblIntegerError').remove();
    if (isNaN(val)) error = "Value must be integer value."
    else if (parseInt(val,10) != val || val<= 0) error = "Value must be non negative number and greater than zero";
    else return true;
    $('#txtNumber').after('<label class="Error"  id="lblIntegerError"><br/>'+error+'</label>');
    return false;
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0
if (isNaN($('#txtColumn').val() <= 0))

That's not right..

You need cast the value to an integer since you're checking against an integer

var intVal = parseInt($('#txtColumn').val(), 10);  // Or use Number()

if(!isNaN(intVal) || intVal <= 0){
   return false;
}
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
  • @Matt Lo : Yes, exactly. Thanks :) – Robin Maben Jul 30 '12 at 05:23
  • But that doesn't work if the user enters invalid data after a valid integer. E.g., `parseInt("123.45abcdefg",10)` returns `123`. Also the radix should be specified (second parameter), or `parseInt("0x12")` returns `18` and `parseInt("010")` returns `8`... – nnnnnn Jul 30 '12 at 05:33
  • @nnnnnn: Then you would need to specify the base. `parseInt('010',10)` or else use `Number('010')`. `parseInt()` uses base 8 as the default. See [this SO question](http://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bug) for more on that. – Robin Maben Jul 30 '12 at 05:37
  • I know how `parseInt()` works, and it uses base 10 as the default except where the string starts with `0` or `0x`. But even when you specify the radix it still happily converts a string like "3abc" to the number 3, so for user-entered data `parseInt()` is not enough on its own... – nnnnnn Jul 30 '12 at 05:45
  • @nnnnnn: The `~~` double negation hack would apply here, then? And sorry, I didn't mean to teach you how parseInt works. Was just putting it there for the OP's reference. It was wrongly addressed.. – Robin Maben Jul 30 '12 at 05:47
0

This should work:

$('#txtNumber').keyup(function() {
    var num = $(this).val();
    num = new Number(num);
    if( !(num > 0) )
        $('#txtNumber').after('<label class="Error"  id="lblIntegerError"><br/>Value must be non negative number and greater than zero.</label>');
});

Note: The parseInt() ignores invalid characters if the first character is numeric but the Number() take cares of them also

M. Ahmad Zafar
  • 4,881
  • 4
  • 32
  • 44
0
$('#txtNumber').keyup(function() 
{
    $('#lblIntegerError').remove();
    if (!isNaN(new Number($('#txtNumber').val())))
    {
        if (parseInt($('#txtNumber').val()) <=0) 
        {
              $('#txtNumber').after('<label class="Error"  id="lblIntegerError"><br/>Value must be non negative number and greater than zero.</label>');
            return false;
        }


    }
    else
     {
          $('#txtNumber').after('<label class="Error"  id="lblIntegerError"><br/>Value must be integer value.</label>');
            return false;
        }
});​