“Show password as text” control

2019-01-25 12:34发布

问题:

I have a usual login form consisting of two input fields, one for login, one for password. I am currently trying to add a control that will show entered password as plain text, so user can check it for typos.

The problem is that browsers (at least Firefox) do not allow dynamic changing of type attribute of input fields, so I cannot just change type="password" to type="text". Another problem is that browsers do not allow to get value of password field, so I can't create a new input type="text" and set its value to the password's one. I've seen several different approaches to this task, including this one, but they are working only if the password is typed and fail when browser autofills the password.

So, any suggestions to do this are welcome. I am using jQuery.

回答1:

You can do something like this:

<input type="password" id="password">
<input type="checkbox" onchange="document.getElementById('password').type = this.checked ? 'text' : 'password'"> Show password


回答2:

If I may, I don't think it's a great idea to show the password in text, for the following reasons:

  1. It's not commonly done, so it will be confusing to the user
  2. It means you are open to over-the-shoulder viewing of the password

I also think, if you just want to help users avoid typos, give them more chances before the password is disabled. I think the typical "3" that most sites implement is not really required, I'd suggest "10" attempts, or perhaps "5", if you wish to be really conservative, is quite acceptable. Just count it down for them, and let them resolve typos on their own.

Just my humble opinion.



回答3:

I have never tried this myself but can't you just access the value property of the element?

if you have something like...

<input id="pw" name="pw" type="password" />

Then in JavaScript / jQuery...

var pass = document.getElementById('pw').value;

$('pw').val()


回答4:

There is no any possibility to show autofilled password for security reasons. Anyone could see your password on your computer for this page if this is possible.

You have to deal with following for complete solution:

  • javascript is not allowed - then you should not display choose password checkbox
  • autocomplete is turned on - as I wrote, you're not able to show password filled this way. Eaighter switch off autocomplete or hide show password until user re-type password.

Autocomplete switch off by this jQuery

$('input').attr('autocomplete', 'off');

For adding checkbox on the fly you can use following jquery-showPassword plugin available at http://www.cuptech.eu/jquery-plugins/



回答5:

$('.show-password').change(function() {
  if ($("#form-fields_show-password").attr('checked')) {
    var showValue = $('#form-fields_password').val();
    var showPassword = $('<input type="text" size="50" required="required" name="form-  fields[password]" id="form-fields_password" class="password required"   value="'+showValue+'">');
    $('#form-fields_password').replaceWith(showPassword);
  } else {
    var hideValue = $('#form-fields_password').val();
    var hidePassword = $('<input type="password" size="50" required="required" name="form-fields[password]" id="form-fields_password" class="password required" value="'+hideValue+'">');
    $('#form-fields_password').replaceWith(hidePassword);
  }
});

Something like this will find the input area, and store the value in a variable called showValue. Then you can replace the element with type="password", with new html where type="text" and if the checkbox is unchecked the value will be dropped into password type field.

There is a problem with this method in that the password type value will be visible in the code, however to get round this you can always remove the value attribute from the password type and just force the user to re-type. If you can live with that in you application.



回答6:

function change(){
id.type="password";
}


<input type="text" value="123456" id="change">
<button onclick="pass()">Change to pass</button>
<button onclick="text()">Change to text</button>
<script>function pass(){document.getElementById('change').type="password";} function text(){document.getElementById('change').type="text"; } </script>


回答7:

Password: <input type="password" value="" id="myInput"><br><br>
<input type="checkbox" onclick="myFunction()">Show Password

<script>
function myFunction() {
    var x = document.getElementById("myInput");
    if (x.type === "password") {
        x.type = "text";
    } else {
        x.type = "password";
    }
}
</script>