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

JS control text box prohibits input of special characters

jacklove
jackloveOriginal
2018-06-15 16:21:003302browse

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;)

 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;)">

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;)">

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;)">

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;)">

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;)">

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;)">

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;)">

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;)">


输入之后立即清除:

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

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

//验证输入框内不能输入特殊字符,输入前先作判断
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();
        }
    }
}

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

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

本文讲解了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!

Statement:
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