Does HTML5 have any kind of text field masking or do I still have to trap onkeydown
etc.?
jbabey is right--"masking" as in blocking certain illegal characters, not hiding what's typed.
The best (as in simplest and most reliable) way I've found is to trap onkeyup
and then just run a regex replace on the value of the textfield, removing any illegal characters.
This has a few advantages:
- It's easy to implement (one function, two lines of code).
- It's reliable and covers all cases I've thought of.
- It doesn't block key commands like copy/paste, select all or arrow keys.
But its major disadvantage is it shows the typed character(s) briefly before removing them, which makes it look very hackish and unprofessional.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…