簡體   English   中英

如何在JavaScript中找到一個小於108且大於0的值:腳本中出現錯誤

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

我編寫以下代碼進行檢查:

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("");
            }

一切都很好,但是,例如,當用戶鍵入1242fdlfldsf表示它是1242但她接受時。 我該如何通過以下代碼來拋棄該值:23232dkfj

parseInt()應該始終與第二個參數一起使用,以便告訴您期望以10為底的數字:

parseInt(num, 10)

另外,您不保存其結果,因此您要比較字符串。

和單| 是按位運算符。 您可能想要||

另外,在進行OR比較時,您需要使用||。 不是|:

if (num < 1 || num > 249)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM