How to determine if a key change event occurs on a printed character?

We are trying to prevent users from entering text beyond the maximum characters that our database allows us to use text field fields in our web application. Once they have reached the maximum length allowed in the text area, we would still like them to hit keys that are not printed, for example: Tab, backspace, ctrl + s, etc.

I am wondering if there is an easy way to determine if the keyboard code is a printable character. I thought something like String.fromCharCode might do the trick and return false if it can't perform the conversion, but doesn't seem to behave like that.

+4
source share
2 answers

Try the following: http://www.quirksmode.org/dom/maxlength.html

Quirksmode uses an easy way to implement the maxlength attribute in text areas, which is not supported.

And to directly answer your question:

 var character = String.fromCharCode(e.charCode); 

Where e is the keypress event event keypress .

+1
source

You can simply set the length of the text field to the maximum number of characters allowed by the database

W3schools

0
source

All Articles