Javascript实现禁止输入中文或英文的例子
本教程教你如何使用Javascript限制输入英文,或限制输入中文。
禁止输入英文:
<inputtype="text"onkeypress="returnevent.keyCode>=48&&event.keyCode<=57||(this.value.indexOf('.')<0?event.keyCode==46:false)"onpaste="return!clipboardData.getData('text').match(/\D/)"ondragenter="returnfalse">
用正则表达式限制只能输入中文:
<inputtype="text"onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"/>
1.用正则表达式限制只能输入全角字符:
<inputtype="text"onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"/>
2.用正则表达式限制只能输入数字:
<inputtype="text"onkeyup="value=value.replace(/[^\d]/g,'')"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/>
3.用正则表达式限制只能输入数字和英文:
<inputtype="text"onkeyup="value=value.replace(/[\W]/g,'')"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/>
热门推荐
10 分别发的祝福语简短
11 大外甥高考祝福语简短
12 小学毕业校长简短祝福语
13 搞工程发财祝福语简短
14 冬季祝福语男生文案简短
15 同事生子问候祝福语简短
16 退休文案寄语简短祝福语
17 送给自己的祝福语简短
18 简短祝福语毛笔字