简体   繁体   中英

input only allow english and non-english character javascript?

How can I make input box for name only allow both E and non-E character, not special character on keydown event.

For example: Accept: a â è ㅎ 啊 Deny: . \\ | } ) # +

And also I don't know why:

'.'.charCodeAt(0) = 46 (chrome console)

In my keydown event if I type dot (.)

 e.which = 190

if I type dot (.) in numlock keyboard

e.which = 110

so confuse.

Try this. Seems it works fine with jQuery 1.4.2, FF, IE, Chrome.

<script>
    $("input").keypress(function (e) {
        if (e.which !== 0 &&
            !e.ctrlKey && !e.metaKey && !e.altKey) {
            alert(String.fromCharCode(e.which));
        }
    });
</script>

You can use this regex to replace all special characters from String :

String.replace(/[\u0021-\u002e\u003a-\u0040\u005b-\u0060\u007b-\u007e]*/g, "")

Similarly you can use this regex to find if user's entered text matches the regex and return false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM