0

我想创建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"/>

谁能帮我。

谢谢。

4

2 回答 2

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"/>
于 2013-10-15T10:00:54.923 回答
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"/>
于 2013-10-15T10:06:42.947 回答