I am trying to edit some existing JavaScript validation code.
Using the onkeydown
event, the code checks that the value in a textbox does not go past a maximum length. I cannot use <input maxlength="value" />
as there may be some formatting characters in the string that I can safely exclude from the maximum length.
The code works fine apart from when the user has pressed the insert key to turn overtype on and they have reached the maximum length. When this occurs if they place the cursor before a character and try to overwrite it the validation thinks that this will go over the limit and doesn't realise that a character will actually be replaced.
This answer states that I cannot detect if overtype is on, but doesn't provide any references. So assuming that I cannot detect overtype, is there anyway in the onkeydown
event to detect if a character is going to be replaced.
I am happy with an IE only solution.
Update: onblur
is not appropriate as this will let them go many characters over the limit before warning them of the maximum length. I would like to prevent them from going over the limit.