1.客户端代码 < script > function check() { if(!(event.keyCode>=48&&event.keyCode<=57)) { alert("此处只接数字!"); event.srcElement.focus(); return false; } if(event.srcElement.value.length==8) { alert("最多输入8个数字!"); event.srcElement.focus(); return false; }} </ script > 2.客户端调用 < input type = " text " name = " mytxt " onkeypress = " return check() " > 转载于:https://www.cnblogs.com/zhouxiaxue/archive/2006/05/24/408320.html