addEvent(window,'load',function () { var fm = document.getElementById('MyForm'); var user = fm.elements['user']; var content = fm.elements['content']; addEvent(content,'keypress',function (evt) { var e = evt || window.event; var charCode = getCharCode(evt); //正则表达式来获取文本是否为数字 //alert(String.fromCharCode(charCode)); if (! /\d/.test(String.fromCharCode(charCode)) && charCode > 8 ){ preDef(evt); //屏蔽非数字键盘的输入 } //PS: charCode > N, N只限于放开光标键、退格键和删除键,N过大,就会出现漏网之鱼。 }); addEvent (areaField,'cut', function (evt) { preDef(evt); //阻止裁剪 }); addEvent (areaField,'copy', function (evt) { preDef(evt); //阻止复制 }); addEvent (areaField,'paste', function (evt) { preDef(evt); //阻止粘贴 }); //屏蔽中文输入法,但Chrome无效 content.style.imeMode = 'disabled'; //【值得注意】: 屏蔽输入法用户体验不好,会让用户误以为电脑坏了 });
addEvent(window,'load',function () { var fm = document.getElementById('MyForm'); var user = fm.elements['user']; var content = fm.elements['content']; addEvent(content,'keypress',function (evt) { var e = evt || window.event; var charCode = getCharCode(evt); //正则表达式来获取文本是否为数字 //alert(String.fromCharCode(charCode)); if (! /\d/.test(String.fromCharCode(charCode)) && charCode > 8 ){ preDef(evt); //屏蔽非数字键盘的输入 } //【注意】: charCode > N, N只限于放开光标键、退格键和删除键,N过大,就会出现漏网之鱼。 }); addEvent(content,'keyup',function (evt) { this.value = this.value.replace(/[^\d]/g,''); //将非数字键替换为空 });
原文地址:http://blog.csdn.net/wangqingbo0829/article/details/46292147