簡體   English   中英

文本框的數字驗證

[英]number validation for textbox

我要驗證文本框應該只允許數字並且只能輸入兩位數。 例如,EIN(員工識別號)只有2位數字,並檢查鍵入的值是數字。 我寫了用於數字驗證的代碼,我只允許輸入2位數字。 我附上了驗證碼

  var specialKeys = new Array();
    specialKeys.push(8); //Backspace
    function IsNumeric1(e) {
        var keyCode = e.which ? e.which : e.keyCode
        var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
        document.getElementById("error1").style.display = ret ? "none" : "inline";
        return ret;
    }

在此處輸入圖片說明

干得好。!

對於僅包括2位數字,可以在文本框中將其設置為Maxlength=2 ,並且僅允許數字。

在此處查看演示

嘗試這個:

if (isNaN(parseInt($("#numb").val()))){
    alert('It must be numbers');
    return false;
}

並添加:

<input type="text" maxlength=2 id="numb">

看,如果行得通。

使用簡單的javascript和RegExp

 var onlyDigits = function(box) { box.value = box.value.replace(/\\D/g, ''); //replace all other than digit }; 
 EIN <input type='text' onkeypress='onlyDigits(this)' onkeyup='onlyDigits(this)' maxlength='2' /> <br/>Only Digit <input type='text' onkeypress='onlyDigits(this)' onkeyup='onlyDigits(this)' /> 

暫無
暫無

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

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