使用2个输入或更改输入类型来显示密码

时间:2017-05-23 11:44:54

标签: javascript html

我想在我的表单中添加show password复选框。 当用户检查显示复选框密码时。 我发现的大多数示例都使用2个输入,一个带有类型文本,另一个带有类型密码。并根据复选框的状态在此输入之间切换。 更改输入类型更简单,那么为什么人们使用2个输入?

3 个答案:

答案 0 :(得分:1)

使用type =“ text”作为显示密码的方式时要小心,因为它会使用户暴露在自动完成设置中以纯文本格式保存密码。我认为两个输入框方法可能更安全,因为您可以使用autocomplete =“ off”

来阻止文本被自动完成功能拾取

请参阅以下描述漏洞的文章:https://www.foxtonforensics.com/blog/post/uncovering-plain-text-passwords-in-internet-history

答案 1 :(得分:0)

我希望您试图询问您是否需要单个密码输入字段并显示密码按钮...以下是我的回答

  <input type="password" name="passwd" id="txtPassword" placeholder="Password"  required="required">
 <input type="checkbox" id="showhide"/>
  <label for="showhide" id="showhidelabel">Show Password</label>




     <script type="text/javascript">
     $(document).ready(function () {
     $("#showhide").click(function () {
     if ($("#txtPassword").attr("type")=="password") {
     $("#txtPassword").attr("type", "text");
     }
     else{
      $("#txtPassword").attr("type", "password");
     }
   });
  });

答案 2 :(得分:0)

可能使其适用于旧版本的IE,因为IE 9及更低版本,不允许动态更改type="password"type="text"。它会抛出错误“无法获取类型属性”