How do you tell if a browser has auto filled a text-box? Especially with username & password boxes that autofill around page load.
My first question is when does this occur in the page load sequence? Is it before or after document.ready?
Secondly how can I use logic to find out if this occurred? Its not that i want to stop this from occurring, just hook into the event. Preferably something like this:
if (autoFilled == true) {
} else {
}
If possible I would love to see a jsfiddle showing your answer.
Possible duplicates
DOM event for browser password autofill?
Browser Autofill and Javascript triggered events
--Both these questions don't really explain what events are triggered, they just continuously recheck the text-box (not good for performance!).
try in CSS
input:-webkit-autofill { border-color: #9B9FC4 !important; }
If you only want to detect whether auto-fill has been used or not, rather than detecting exactly when and to which field auto-fill has been used, you can simply add a hidden element that will be auto-filled and then check whether this contains any value. I understand that this may not be what many people are interested in. Set the input field with a negative tabIndex and with absolute coordinates well off the screen. It's important that the input is part of the same form as the rest of the input. You must use a name that will be picked up by Auto-fill (ex. "secondname").
Append this input to the form and check its value on form submit.
This works for me in the latest Firefox, Chrome, and Edge:
The problem is autofill is handled differently by different browsers. Some dispatch the change event, some don't. So it is almost impossible to hook onto an event which is triggered when browser autocompletes an input field.
Change event trigger for different browsers:
For username/password fields:
For other form fields:
You best options are to either disable autocomplete for a form using
autocomplete="off"
in your form or poll at regular interval to see if its filled.For your question on whether it is filled on or before document.ready again it varies from browser to browser and even version to version. For username/password fields only when you select a username password field is filled. So altogether you would have a very messy code if you try to attach to any event.
You can have a good read on this HERE
I had the same problem and I've written this solution.
It starts polling on every input field when the page is loading (I've set 10 seconds but you can tune this value).
After 10 seconds it stop polling on every input field and it starts polling only on the focused input (if one). It stops when you blur the input and again starts if you focus one.
In this way you poll only when really needed and only on a valid input.
It does not work quite well when you have multiple values inserted automatically, but it could be tweaked looking for every input on the current form.
Something like:
I used the blur event on the username to check if the pwd field had been auto-filled.
This works for IE, and should work for all other browsers (I've only checked IE)