boylesg Posted February 1, 2014 Share Posted February 1, 2014 <script> function doOnKeyPrerss(Event) { alert(Event.charCode); alert(Event.keyIdentifier); } </script> Neither of these work - I just get 'undefined'. So how do you obtain the key value? Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2014 Share Posted February 1, 2014 How is the function being called? If it's through an attribute on the element then it won't work. The event handler has to be assigned through code: element.addEventListener("keypress", doOnKeyPress, false);// For IE 8 and underelement.attachEvent("onkeypress", doOnKeyPress) Link to comment Share on other sites More sharing options...
boylesg Posted February 1, 2014 Author Share Posted February 1, 2014 I think I can forget the idea of field validation in javascript altogether. It appears to be impossibly cumbersome. I think I will just stick with field validation in php. Link to comment Share on other sites More sharing options...
Hadien Posted February 1, 2014 Share Posted February 1, 2014 to find the key that is pressed use something like this: var key = event.key || event.keyCode || event.which;alert(key); event.key seems to be the upcoming standard way of getting the key, but hasn't been implemented as of yet. keyCode and which are the current recommendations of grabbing the character, yet they are slated to be depreciated and won't be recommended soon. the code I show here "should" give you the right property regardless of version or platform. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now