After countless hours of troubleshooting, I am convinced that something is awry.
My issue revolves around a simple input field:
<input type="number">
I intend for it to exclusively accept numbers (0-9) and nothing else. To accomplish this, I implemented an event listener to monitor the event's keycode.
While everything functions smoothly across most platforms, Android presents a challenge. The
attribute triggers a numeric keyboard, which is beneficial, but also allows other characters like commas, dots, and hyphens. When one of these special characters is pressed, the behavior becomes erratic:type="number"
- The onkeypress event fails to trigger at all (why?)
- The onkeydown event fires, and the specified function executes. However, the browser has already inserted the character (why?), preventing the use of the preventDefault method on the event.
- The input event fires as well, but the "code" and "which" properties of the event object are undefined. Once again, the browser has preemptively inputted the character.
What am I overlooking?
In my AngularJs-powered environment, I have configured event listeners within a directive. Unfortunately, replicating the process with jQuery doesn't resolve the issues.
Suggestions have been made to utilize "$parsers" to filter out unwanted characters and push only the desired values. Yet, this approach seems ineffective because the input type is "number," and the browser expects solely numerical inputs (implying an error in how Android browsers handle this).
For a demonstration, refer to this working Fiddle, ideally accessed on an Android device.