The problem I have is that I need to validate a text field so that it only accepts me ";"
and "_"
. So far it doesn't work for me with the code I have.
Code:
function doNotSubmitFormOnEnterPress(event) {
if (event.keyCode === 188 ) {
return false;
}
return true;
}
You can do this with Jquery , You can validate if the Shift key is pressed , with the property shiftKey , it will return a boolean value if the key is pressed or not
I suggest using
keypress
instead ofkeyDown
so it doesn't limit actuation of ctrl keys , such as:Backspace
,Delete
, Etc. Although I lean toward using jQuery as well, I instead show for illustrative purposes the use of addEventListener() to handle the eventonKeyPress
associated to the<input>
, as well as the use of event.preventDefault() , to prevent the normal flow of the<input>
in case of not typing the required values.Example: