0

I write this code for checking:

var num = $("#knum").val();
        if (isNaN(parseInt(num))) {
            $("#errorknum").html("Please define a number.");
        }
        else {
            if (num < 1 | num > 249) {
                $("#errorkrnum").html("your number must be less then 249 and can be greater than 1");
            }
            else {
                $("#errorknum").html("");
            }

Everything is fine but, for example, when the user type 1242fdlfldsf means it is 1242 but she accept it. How can I dishandle this value by code like for example: 23232dkfj

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124

2 Answers2

3

parseInt() should always be used with the second argument, in order to tell it that you expect a base-10 number:

parseInt(num, 10)

Also, you don't save its results so you are comparing strings.

And the single | is a bitwise operator. You probably want ||.

Álvaro González
  • 135,557
  • 38
  • 250
  • 339
1

Also, when you do your OR comparison, you need to be using || not |:

if (num < 1 || num > 249)
Matthew Abbott
  • 59,075
  • 9
  • 102
  • 128