2
我在SO上看到過類似的問題,但我的方法略有不同。我有一個textarea
,我想只接受數字作爲輸入。目前所有的輸入都是允許的,並且錯誤警報沒有被觸發。如何僅允許使用jQuery的textarea中的數字?
我認爲問題可能是被按下的鍵沒有被傳遞到我的驗證函數,但我不知道如何做到這一點,而不使用我試圖避免使用的html onkeypress。
HTML:
<textarea id="noteNumberInput" placeholder="Note number"></textarea>
JS:
$(document).ready(function() {
var noteNumberInput = document.getElementById("noteNumberInput");
//VALIDATE NOTE NUMBER TEXTAREA
function validate() {
var keycode = (key.which) ? key.which : key.keyCode;
//comparing pressed keycodes
if (keycode < 48 || keycode > 57) {
alert("Please only enter the note number e.g. '1', '2' etc.")
return false;
}
}
noteNumberInput.addEventListener("keypress", validate);
});
是如何從不同的[如何限制textarea的只持有數?](http://stackoverflow.com/questions/22571923/how-to-limit-the-textarea-to-only-hold-numbers) – Mistalis
看看這個htt電話號碼://stackoverflow.com/questions/8936018/limit-input-to-numbers-and-on-input-field –