如果文本框是一个input如下:
<input type="text" name = "userName" value="<c:out value='${userName}'/>" maxLength = "10" οnkeyup="modifyNumber2(event, this);" οnkeypress= "modifyNumber1(event);" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))" οnpaste="return false" οndragenter="return false" style="ime-mode:disabled;" />
其中:
onkeypress是键盘往下敲代码时触发的动作
onkeyup是键盘抬起来触发的动作(常用作删除操作)
2个js脚本如下.无私奉献,点滴关爱.....哈哈
function modifyNumber1(event) {
event.returnValue=false;
rule = /[0-9]/;
if (rule.test(String.fromCharCode(event.keyCode))) {
event.returnValue=true;
}
return event.returnValue;
}
function modifyNumber2(event, numObject) {
var num = numObject.value;
var rule = /[0-9]/g;
if((event.keyCode>47&&event.keyCode<58) || (event.keyCode>95&&event.keyCode<106) || event.keyCode==8 || event.keyCode==46){
if (rule.test(num)) {
num = parseInt(num,10) ;
}
if( num == 0 ){
numObject.value = '0';
}
numObject.value = num;
}
}
至于后面的事件,还请大侠们多多指教