[英]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;
}
嘗試這個:
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.