1、直接在文本域中写入onkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"
2、另外写函数
<script>
var str="0123456789";
function press()
{
var str1=document.all.fundsname.value;
for(int i=0; i<str1.length; i++){
if (str.indexOf(str1)==-1){
alert("只能输入数字!");
document.all.b1.value="";
return false; } }
}
</script>
3、以上两种形式不能防止中文的输入,防止中文可以采用如下的形式
function fun_submit(){ var strname = document.all.fundsname.value; var strunit = document.all.fundsunit.value; var strmoney = document.all.fundsmoney.value; if(strname.length<=0||strunit.length<=0||strmoney.length<=0){ alert("信息输入不完整"); return; } var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/ if(!re.test(strmoney)){ alert("请输入数字"); return; }else if(strmoney<=0){ alert("请输入大于0的数字"); return; } document.forms[0].action="servlet/AddFundsServlet"; document.forms[0].submit(); }