Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get value of an auto-filled password box in JavaScript?

var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
    inputs[i].onfocus = foo;
}
function foo(){
    alert(this.value);
}

When the input values are manually entered:
Above code works and alerts the correct values regardless of the type of an input field.

When the input values auto-filled by browser:
Code works and alerts the correct values when the input field is of type text. In case of a password field, it alerts empty string!

Is this behaviour because of the browser's security policies? Or there's any workaround possible? I tried it in Chrome browser.

like image 850
Akhil Dixit Avatar asked Feb 19 '16 07:02

Akhil Dixit


People also ask

How to validate password length in html?

The maxLength property sets or returns the value of the maxlength attribute of a password field. The maxlength attribute specifies the maximum number of characters allowed in a password field. Tip: To set or return the width of a password field, in number of characters, use the size property.

Which function is needed to type into the password box element?

<input type="password"> <input> elements of type password provide a way for the user to securely enter a password.


2 Answers

$(document).ready(function() {
  $("input")
      .blur(password)
      .trigger('blur');
});

function password() {
   alert($(this).val())
}

DEMO

like image 180
Rino Raj Avatar answered Nov 02 '22 19:11

Rino Raj


This is an interesting question because I believe that you are referring to the side-effect of a security feature in Chrome.

Chrome (and probably other browsers) may not actually populate the DOM with the auto-filled password because a malicious programmer could write a JavaScript that attempts to steal auto-filled passwords from unsuspecting victims once a page loads.

You will most likely require that the user clicks a submit button before you can gain access to that information.

You might be able to force the form to submit and check the value right before the submit actually occurs. If that works, then you can just cancel the submission by returning false in "onsubmit" and you now have the password.

like image 39
CzechErface Avatar answered Nov 02 '22 19:11

CzechErface