最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript regex to disallow all special characters - Stack Overflow

programmeradmin3浏览0评论

Here is a fiddle to start - /

Javascript code:

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

<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.

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

Javascript code:

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

<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.

Share Improve this question asked Mar 6, 2013 at 9:25 AshwinAshwin 12.4k22 gold badges84 silver badges119 bronze badges 1
  • take a look on this stackoverflow./questions/995183/… – Kotzilla Commented Mar 6, 2013 at 9:36
Add a ment  | 

5 Answers 5

Reset to default 6

[old answer, update] KeyboardEvent.charCode and KeyboardEvent.keyCode are deprecated. We should use KeyboardEvent.key.

document.querySelector(`#validate`).onkeypress = e => /[a-z0-9]/i.test(e.key);
<input type="text" id="validate">

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

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

Try

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

It returns true if there is a match.

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

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

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

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.which and e.charCode value.
See JavaScript Madness: Keyboard Events.

jsFiddle.

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

This Worked for Me. Prevent Users from Entering Special Characters(Except Backspace etc)

PatternValidation(e){

    if(!e.key.match(/^[a-zA-Z0-9]*$/))
       {
         e.preventDefault();
       }
},

This is triggered through binding html attribute with keydown event handler

<input type="text" onkeydown="PatternValidation($event)">
发布评论

评论列表(0)

  1. 暂无评论