jsp中文本框只能输入数字怎么实现?

来源:百度知道 编辑:UC知道 时间:2024/07/07 23:20:37
文本框:<input type=text size="6" name="txtNum">
最好是直接输入的时候就只能输入数字

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
<script>
function blockChar(oText){
sChar = oText.getAttribute("validchar");
ddd = String.fromCharCode(window.event.keyCode);
var res = sChar.indexOf(ddd) > -1;
return res || window.event.ctrlKey;
}
</script>
</head>
<body>
<input type=text validchar="0123456789" onkeypress="return blockChar(this)" onpaste="return false"/>
</body>
</html>
//onpaste 用来防止粘帖的