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
From my personal experience, the below code works well with firefox IE and safari, but isn't working very well at picking autocomplete in chrome.
function check(){
clearTimeout(timeObj);
timeObj = setTimeout(function(){
if($('#email').val()){
//do something
}
},1500);
}
$('#email').bind('focus change blur',function(){
check();
});
Below code works better, because it will trigger each time when user clicks on the input field and from there you can check either the input field is empty or not.
$('#email').bind('click', function(){
check();
});
try in CSS
input:-webkit-autofill {
border-color: #9B9FC4 !important;
}
Just in case someone is looking for a solution (just as I was today), to listen to a browser autofill change, here's a custom jquery method that I've built, just to simplify the proccess when adding a change listener to an input:
$.fn.allchange = function (callback) {
var me = this;
var last = "";
var infunc = function () {
var text = $(me).val();
if (text != last) {
last = text;
callback();
}
setTimeout(infunc, 100);
}
setTimeout(infunc, 100);
};
You can call it like this:
$("#myInput").allchange(function () {
alert("change!");
});
There does appear to be a solution to this that does not rely on polling (at least for Chrome). It is almost as hackish, but I do think is marginally better than global polling.
Consider the following scenario:
User starts to fill out field1
User selects an autocomplete suggestion which autofills field2 and field3
Solution: Register an onblur on all fields that checks for the presence of auto-filled fields via the following jQuery snippet $(':-webkit-autofill')
This won't be immediate since it will be delayed until the user blurs field1 but it doesn't rely on global polling so IMO, it is a better solution.
That said, since hitting the enter key can submit a form, you may also need a corresponding handler for onkeypress.
Alternately, you can use global polling to check $(':-webkit-autofill')
I was reading a lot about this issue and wanted to provide a very quick workaround that helped me.
let style = window.getComputedStyle(document.getElementById('email'))
if (style && style.backgroundColor !== inputBackgroundNormalState) {
this.inputAutofilledByBrowser = true
}
where inputBackgroundNormalState
for my template is 'rgb(255, 255, 255)'.
So basically when browsers apply autocomplete they tend to indicate that the input is autofilled by applying a different (annoying) yellow color on the input.
Edit : this works for every browser
I know this is an old thread but I can imagine many comes to find a solution to this here.
To do this, you can check if the input(s) has value(s) with:
$(function() {
setTimeout(function() {
if ($("#inputID").val().length > 0) {
// YOUR CODE
}
}, 100);
});
I use this myself to check for values in my login form when it's loaded to enable the submit button. The code is made for jQuery but is easy to change if needed.