Home > Web Front-end > JS Tutorial > JS control text box prohibits input of special characters

JS control text box prohibits input of special characters

jacklove
Release: 2018-06-15 16:21:00
Original
3387 people have browsed it

JS 控制不能输入特殊字符

<input type="text" class="domain" onkeyup="this.value=this.value.replace(/[^u4e00-u9fa5w]/g,&#39;&#39;);">
this.value=this.value.replace(/[^u4e00-u9fa5w]/g,&#39;&#39;)
Copy after login

JS 控制文本框只能输入数字

<input onkeyup="value=value.replace(/[^0-9]/g,&#39;&#39;)" onpaste="value=value.replace(/[^0-9]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^0-9]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入数字、小数点

<input onkeyup="value=value.replace(/[^\0-9\.]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\0-9\.]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\0-9\.]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入英文

<input onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\a-\z\A-\Z]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\a-\z\A-\Z]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入英文、数字

<input onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\a-\z\A-\Z0-9]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\a-\z\A-\Z0-9]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入中文

<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\u4E00-\u9FA5]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\u4E00-\u9FA5]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入中文、英文、数字

<input onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入中文、英文、数字、空格

<input onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\ ]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\ ]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\ ]/g,&#39;&#39;)">
Copy after login

JS 控制文本框只能输入中文、英文、数字、小数点

<input onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\.]/g,&#39;&#39;)" onpaste="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\.]/g,&#39;&#39;)" oncontextmenu = "value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\.]/g,&#39;&#39;)">
Copy after login


输入之后立即清除:

//验证输入框内不能输入特殊字符,输入就立刻清除
	 function cleanSpelChar(th){     
	    if(/["&#39;<>%;)(&+]/.test(th.value)){           
	    $(th).val(th.value.replace(/["&#39;<>%;)(&+]/,""));     
	    } 
	}
Copy after login

如果是特殊字符,禁止输入:

//验证输入框内不能输入特殊字符,输入前先作判断
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(/["&#39;<>%;)(&+]/);
    if (txt.test(character)) {
        if (document.all) {
            window.event.returnValue = false;
        } else {
            arguments.callee.caller.arguments[0].preventDefault();
        }
    }
}
Copy after login

JS替换英文特殊字符 `~!@#$%^&*()_+-=;':"\|,./<>?

var subject= document.getElementById("subject").value;
subject = subject.replace(/[\-\_\,\.\!\|\~\`\(\)\#\@\%\-\+\=\/\&#39;\$\%\^\&\*\{\}\:\;\"\L\<\>\?\\]/g, &#39;&#39;); 
alert(subject);
Copy after login

本文讲解了JS控制文本框禁止输入特殊字符,更多相关内容请关注php中文网。

相关推荐:

AngularJS 的相关内容

jQuery Validation使用教程

gulp模块的方法

The above is the detailed content of JS control text box prohibits input of special characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template