Forum Moderators: open
I had the function triggered by onblur() and onkeyup(). All seemed to be satisfactory. Then I got a complaint that the validation wasn't happening when the user hovered the mouse over the input, right-clicked, chose "Paste", and entered text that way.
No problem, I said, I'll just add "onchange" to the list of events that I'm listening for. That ought to do it.
No good! the onchange event isn't being fired. Isn't that strange? the content of the field did change. Why no onchange?
Does anyone here know how to trigger an event when someone pastes using the browser's right-click menu?
Most of the time I just alter my methods - in this case, leave it at onChange and as soon as they navigate away (to submit, or another field) it will pick up the change. Or, just use onBlur(), because in practice, it seems to be equivalent.
I did consider what might be a more eloquent way, but set it aside for another day. Run your validation on a timer. Of course, it would have to "know" the initial load values
if (field != '') { [validate] }
which becomes problematic in editing existing data, but chances are very good if the data exists it's already been validated, so it will probably pass. (The word "probably" always scares me in programming.) You'd also have to have an alert toggle so it doesn't alert at every iteration of the validation, and figure out some way to "record" the valid field states as they get filled out (now you know why I set it aside.)
This is probably a good approach for a validation that validates as you type, but that could also be . . . dangerous and bug-prone, too.