HTML:
<input type="text" size="1" onKeyPress="onlynumbers(this.value);" onMouseOut="onlynumbers(this.value);"></input>
javascript:
//如果不是数字,parseInt(str_value,10)返回NaN。注意不能用 == NaN, 要用 isNaN()。
var str_value = 0;
function onlynumbers(str_value) {
if (isNaN(parseInt(str_value,10))) {
alert('请填写整数数字。');
}
}