I have a field that accepts the year, so I have created
input type="number"
and implemented keydown event to restrict user to enter more than 4 digits.
Now I'm facing an issue and need help in figuring out the logic. Following is the case:
- Enter 4 digits in the textbox
- Select entered text using SHIFT + Arrow Keys
Now if you type a number it should replace the data but since I have barred it, it will not. Need to cover this case.
Also find code in following JSFiddle.
I also have lot of css and validation on input[type=number]
, so cannot change to input[type=text]
.
Also same form is used on mobile devices, and when user selects textbox, numeric keyboard should appear.
Edit 1
while searching for option, I found a JSfiddle that could direct us to right direction.
Issue here also is input[type=number]
does not support selection property. Reference
As an alternative, we have decided to move to input[type=tel]
. This would work in similar fashion, but will allow us to use maxLength attribute. Still if anyone has a better way, please share.