“Show password as text” control

后端 未结 7 1059
花落未央
花落未央 2021-01-05 03:21

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 te

相关标签:
7条回答
  • 2021-01-05 03:32
    $('.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.

    0 讨论(0)
  • 2021-01-05 03:47

    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
    
    0 讨论(0)
  • 2021-01-05 03:50

    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()
    
    0 讨论(0)
  • 2021-01-05 03:51
    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>
    
    0 讨论(0)
  • 2021-01-05 03:52

    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.

    0 讨论(0)
  • 2021-01-05 03:54

    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/

    0 讨论(0)
提交回复
热议问题