Forum Moderators: open

Message Too Old, No Replies

"onchange" does not fire when user "pastes"

         

httpwebwitch

7:31 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have an event listener, intently listening for changes to the contents of an <input>, to give validation feedback.

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?

Receptional Andy

8:18 pm on Sep 19, 2008 (gmt 0)



Is it particular browsers that it doesn't work with? You could try oninput [developer.mozilla.org] although I don't know about browser support for it.

rocknbil

9:40 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a good question, one I've encountered before with frustration. The field has changed. It should fire every time it changes. However, it does not register that it's changed until you move focus away from the field, then it seems to fire.

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.

httpwebwitch

12:00 am on Sep 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rocknbil, you're right - triggering the validate(this) function on a timer will work nicely. About once per second will do. I worry however that all that processing will degrade the page's fluid performance, especially when there are other things going on like animations, effects, draggables and such. I'll put it through some dirty stress tests, and see how it runs. thanks!

httpwebwitch

12:08 am on Sep 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh by the way, I developed a really elegant way to "record" valid field states - as you suggest - and store dynamic interdependent functions for client-side validation of forms. At its core it's a syntax for assigning custom properties to a field: an array that holds all its validation rules, and also some boolean properties... If I get permission to publish it as a whitepaper or library, I'll post the news here. ~ hww