简体   繁体   English

如何在JavaScript中找到一个小于108且大于0的值:脚本中出现错误

[英]How to find that a value is less than 108 and greater than 0 in JavaScript: something wrong in script

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. 一切都很好,但是,例如,当用户键入1242fdlfldsf表示它是1242但她接受时。 How can I dishandle this value by code like for example: 23232dkfj 我该如何通过以下代码来抛弃该值:23232dkfj

parseInt() should always be used with the second argument, in order to tell it that you expect a base-10 number: parseInt()应该始终与第二个参数一起使用,以便告诉您期望以10为底的数字:

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 || 您可能想要|| .

Also, when you do your OR comparison, you need to be using || 另外,在进行OR比较时,您需要使用||。 not |: 不是|:

if (num < 1 || num > 249)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM