javascript javascript正则表达式禁止所有特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15243387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:01:17  来源:igfitidea点击:

javascript regex to disallow all special characters

javascriptjqueryregexkeycode

提问by Ashwin

Here is a fiddle to start - http://jsfiddle.net/zuVpx/1/

这是一个开始的小提琴 - http://jsfiddle.net/zuVpx/1/

Javascript code:

Javascript代码:

<script>
    function validate(e) {
        var regex = new RegExp("[a-zA-Z0-9]");
        var key = e.keyCode || e.which;
        key = String.fromCharCode(key);

        if(!regex.test(key)) {
            e.returnValue = false;
            if(e.preventDefault) {
                e.preventDefault();
            }
        }
    }
</script>

HTML code:

HTML代码:

<input type="text" onkeypress="validate(event)" />

I want only characters and numbers. Keys like backspace, delete, capslock and arrowkeys etc should work.

我只想要字符和数字。退格、删除、大写锁定和箭头键等键应该可以工作。

Thanks in advance.

提前致谢。

回答by KooiInc

Add an idto the input (e.g. 'validate') and use:

id在输入中添加一个(例如“验证”)并使用:

document.querySelector('#validate').onkeypress = validate;

function validate(e) {
        e = e || event;
        return /[a-z0-9]/i.test(
                   String.fromCharCode(e.charCode || e.keyCode)
               ) || !e.charCode && e.keyCode  < 48;
}

JSFiddle

JSFiddle

回答by WillyMilimo

Try

尝试

/[-!$%^&*()_+|~=`\#{}\[\]:";'<>?,.\/]/.test(your_variable)

It returns true if there is a match.

如果匹配,则返回 true。

回答by Dve

How about just using an additional if clause? Something like...

只使用一个额外的 if 子句怎么样?就像是...

key.charCodeAt(0) > 32

So...

所以...

function validate(e) {
    var regex = new RegExp("[a-zA-Z0-9]");
    var key = e.keyCode || e.which;
    key = String.fromCharCode(key);

    if(!regex.test(key) && key.charCodeAt(0) > 32) {
        e.returnValue = false;
        if(e.preventDefault) {
            e.preventDefault();
        }
    }
}

回答by MikeM

To overcome the problem that for example the left arrow key produces the same keyvalue as the %key, you could use

为了克服例如向左箭头键产生与键相同的key值的问题%,您可以使用

function validate(e) {
    e = e || window.event;
    var bad = /[^\sa-z\d]/i,
        key = String.fromCharCode( e.keyCode || e.which );   

    if ( e.which !== 0 && e.charCode !== 0 && bad.test(key) ) {
        e.returnValue = false;
        if ( e.preventDefault ) {
            e.preventDefault();
        }
    } 
 }   

Any printable character should produce a non-zero e.whichand e.charCodevalue.
See JavaScript Madness: Keyboard Events.

任何可打印字符应该产生一个非零e.whiche.charCode值。
请参阅JavaScript 疯狂:键盘事件

jsFiddle.

js小提琴

The above assumes spaces are valid - if not, just remove the \sfrom the negated character class.

以上假设空格有效 - 如果不是,只需\s从否定字符类中删除。