您的位置:首页 > Web前端 > JavaScript

javascript 控制input只允许输入的各种指定内容

2017-11-08 16:01 621 查看
老是遇到一些校验

1.只允许输入数字

<input name="username" type="text"
onkeyup="value=this.value.replace(/\D+/g,'')">


2.只允许输入输入大小写英文字母,数字和下划线

方法一:

<input name="username" type="text"
onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">


方法二:

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^a-zA-Z0-9_]/g,'');">


3.只允许输入汉字

<input name = "name" type = "text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">


4.只允许输入英文和数字

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9]/g,'');">


5.只允许输入问问字幕,数字和=&%

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^a-zA-Z0-9=&%]/g,'');">


6.不能输入特殊字符

<input name = "name" type = "text" onkeyup="value=value.replace(/[\W]/g,'')
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">


7.只能输入数字和小数点

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^\0-9\.]/g,'');">


8.只允许输入英文

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'');">


9.只允许输入英文,数字,中文

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'');">


10.只允许输入英文,数字,中文,小数点

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\.]/g,'');">


11.只允许输入英文,数字,中文,小数点,下划线

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\._]/g,'');">


12.只允许输入英文,数字,中文,小数点,下划线,空格

<input name  = "username" type = "text" onkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\._ ]/g,'');">










                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  input