1.只允許輸入數字
<input name="username" type="text" οnkeyup="value=this.value.replace(/\D+/g,'' )">
2.只允許輸入輸入大小寫英文字母,數字和下劃線
方法一:
<input name="username" type="text" οnkeyup="value=value.replace(/[^\w\.\/]/ig,'')">
方法二:
<input name = "username" type = "text" οnkeyup="this.value=this.value.replace(/[^a-zA-Z0-9_]/g,'');">
3.只允許輸入漢字
<input name = "name" type = "text" οnkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">
4.只允許輸入英文和數字
<input name = "username" type = "text" οnkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9]/g,'');">
5.只允許輸入問問字幕,數字和=&%
<input name = "username" type = "text" οnkeyup="this.value=this.value.replace(/[^a-zA-Z0-9=&%]/g,'');">
6.不能輸入特殊字符
<input name = “name” type = “text” οnkeyup="value=value.replace(/[\W]/g,’’)
onbeforepaste=“clipboardData.setData(‘text’,clipboardData.getData(‘text’).replace(/[^\d]/g,’’))”>
7.只能輸入數字和小數點
<input type="text" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')" />
8.只允許輸入英文
<input name = "username" type = "text" οnkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'');">
9.只允許輸入英文,數字,中文
<input name = “username” type = “text” οnkeyup=“this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,’’);”>
10.只允許輸入英文,數字,中文,小數點
<input name = "username" type = "text" οnkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\.]/g,'');">
11.只允許輸入英文,數字,中文,小數點,下劃線
<input name = "username" type = "text" οnkeyup="this.value=this.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\._]/g,'');">
- 禁止輸入空格:
<input type="text" onkeyup="this.value = this.value.trim()" />
- 禁止特殊字符串: 這裏禁止輸入: <>?
<input type="text" onkeyup="this.value=this.value.replace(/[<>?]/,'')" />
這裏記了一些常用的正則:
https://blog.csdn.net/freedomVenly/article/details/78732923