I want to show an error message to a user, if he enters more characters in a text field than specified and this specification may change with the initial characters provided. Along with that, I need to process each character that user enters for some business purposes. e.g.
Let's think of a credit card field.
Different credit card types have different Max Lengths. I would like to stop users from entering more characters in the field than their credit card type allows.
However, the credit card type can be detected only when the user provides few initial characters of the credit card.
My approach here is to try to detect credit card on every key press, and set the maxlength of the field according to credit card type detected. If not detected, keep the max length to 19 (which is maximum for all)
What I have tried
---------------------------------------------------------------------
| Event | Problem |
| ------------------------------------------------------------------|
| | Change will not be triggered until the user |
| OnChange | moves out of the field. Since I need this event |
| | on every char entered, this is not helpful |
|-------------------------------------------------------------------|
| | It gives issues when a user presses the key for a |
| KeyDown | longer duration. Characters are entered multiple |
| | times, but this event is triggered only once. |
|-------------------------------------------------------------------|
| KeyUP | Same as above. |
|-------------------------------------------------------------------|
| KeyPress | I am getting older value. The newly added character |
| | is not available. Hence incorrect calculation |
---------------------------------------------------------------------
I have an alternative of using time-out, but looking for better options.
My Question is different than this one. because of above mentioned reasons.
Please suggest a good solution for this.