views:

90

answers:

3

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

+3  A: 

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 G. Vicario
+1  A: 

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

if (num < 1 || num > 249)
Matthew Abbott
A: 

Take a look at this SO question: http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric

sirhc
Thanks for give me a link. it is help me and clear all query about it.
steven spielberg