1.标签上直接替换方法:
JS 控制不能输入特殊字符
1
2
3
|
<input type="text"class="domain"onkeyup="this.value=this.value.replace(/[^u4e00-u9fa5w]/g,'')"; this.value=this.value.replace(/[^u4e00-u9fa5w]/g,'') |
JS 控制文本框只能输入数字
1
|
< input onkeyup = "value=value.replace(/[^0-9]/g,'')" onpaste = "value=value.replace(/[^0-9]/g,'')" oncontextmenu = "value=value.replace(/[^0-9]/g,'')" > |
JS 控制文本框只能输入数字、小数点
1
|
< input onkeyup = "value=value.replace(/[^ -9.]/g,'')" onpaste = "value=value.replace(/[^ -9.]/g,'')" oncontextmenu = "value=value.replace(/[^ -9.]/g,'')" > |
JS 控制文本框只能输入英文
1
|
< input onkeyup = "value=value.replace(/[^a-zA-]/g,'')" onpaste = "value=value.replace(/[^a-zA-]/g,'')" oncontextmenu = "value=value.replace(/[^a-zA-]/g,'')" > |
JS 控制文本框只能输入英文、数字
1
|
< input onkeyup = "value=value.replace(/[^a-zA-0-9]/g,'')" onpaste = "value=value.replace(/[^a-zA-0-9]/g,'')" oncontextmenu = "value=value.replace(/[^a-zA-0-9]/g,'')" > |
JS 控制文本框只能输入中文
1
|
< input onkeyup = "value=value.replace(/[^u4E00-u9FA5]/g,'')" onpaste = "value=value.replace(/[^u4E00-u9FA5]/g,'')" oncontextmenu = "value=value.replace(/[^u4E00-u9FA5]/g,'')" > |
JS 控制文本框只能输入中文、英文、数字
1
|
< input onkeyup = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5]/g,'')" onpaste = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5]/g,'')" oncontextmenu = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5]/g,'')" > |
JS 控制文本框只能输入中文、英文、数字、空格
1
|
< input onkeyup = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5 ]/g,'')" onpaste = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5 ]/g,'')" oncontextmenu = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5 ]/g,'')" > |
JS 控制文本框只能输入中文、英文、数字、小数点
1
|
< input onkeyup = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5.]/g,'')" onpaste = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5.]/g,'')" oncontextmenu = "value=value.replace(/[^a-zA-0-9u4E00-u9FA5.]/g,'')" > |
2.输入之后立即清除:
1
2
3
4
5
6
7
8
|
//验证输入框内不能输入特殊字符,输入就立刻清除 function cleanSpelChar(th){ if (/[ "'<>%;)(&+]/.test(th.value)){ $(th).val(th.value.replace(/[" '<>%;)(&+]/, "" )); } } |
3.如果是特殊字符,禁止输入:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//验证输入框内不能输入特殊字符,输入前先作判断 function processSpelChar(){ var code; var character; if (document.all){ code = window.event.keyCode; } else { code = arguments.callee.caller.arguments[0].which; } var character = String.fromCharCode(code); var txt = new RegExp(/["'<>%;)(&+]/); if (txt.test(character)){ if (document.all){ window.event.returnValue = false ; } else { arguments.callee.caller.arguments[0].preventDefault(); } } } |