我想创建TextField
它只允许数字。为此,我编写了以下代码,但它不起作用。
脚本代码:
function numberTextField(){
var keyAsciiValue=event.keyCode||event.which;
console.log(keyAsciiValue);
if (keyAsciiValue>=48&&keyAsciiValue<=57) {
console.log("This is Number");
return true;
}else{
console.log("This is Not a Number");
return false;
};
}
html代码:
<input type="text" onkeyup="numberTextField()" placeholder="NoOfRows" id="fieldsCount2"/>
任何人都可以帮助我。
感谢。
答案 0 :(得分:0)
function numberTextField(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
console.log("This is Not a Number");
return false;
}
else
{
console.log("This is a Number");
return true;
}
}
和HTML
<input type="text" onkeypress="return numberTextField(event)" placeholder="NoOfRows" id="fieldsCount2"/>
答案 1 :(得分:0)
更新代码:
<input type="text" value="" onkeypress="this.value = this.value.replace(/[^0-9]$/,'')" onkeyup="this.value = this.value.replace(/[^0-9]$/,'')" placeholder="NoOfRows" id="fieldsCount2"/>