如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?


当前回答

大多数主要浏览器和密码管理器(正确地说,IMHO)现在都忽略了autocomplete=off。

为什么?许多银行和其他“高安全性”网站“出于安全目的”在其登录页面中添加了autocomplete=off,但这实际上降低了安全性,因为这会导致人们更改这些高安全性网站上的密码,以便于记忆(从而破解),因为自动完成功能已被破坏。

很久以前,大多数密码管理器开始忽略autocomplete=off,现在浏览器开始只对用户名/密码输入执行相同的操作。

不幸的是,自动完成实现中的错误会将用户名和/或密码信息插入到不适当的表单字段中,从而导致表单验证错误,或者更糟的是,意外地将用户名插入到用户故意留空的字段中。

web开发人员应该做什么?

如果您可以单独保存页面上的所有密码字段,这是一个很好的开始,因为密码字段的存在似乎是用户/密码自动完成的主要触发因素。否则,请阅读下面的提示。Safari注意到有两个密码字段,并在这种情况下禁用自动完成,假设它必须是一个更改密码表单,而不是登录表单。因此,请确保在允许的任何表单中使用两个密码域(新的和确认新的)不幸的是,Chrome34每当看到密码字段时,都会尝试使用user/pass自动填充字段。这是一个非常糟糕的bug,希望他们会改变Safari的行为。但是,将其添加到表单顶部似乎会禁用密码自动填充:<input-type=“text”style=“display:none”><input-type=“password”style=“display:none”>

我还没有彻底调查IE或Firefox,但如果其他人在评论中有信息,我很乐意更新答案。

其他回答

始终有效的解决方案

我用随机字符解决了与谷歌Chrome的无休止斗争。当您总是使用随机字符串呈现自动完成时,它将永远不会记住任何内容。

<input name="name" type="text" autocomplete="rutjfkde">

希望这对其他人有所帮助。

2022年更新:

Chrome做出了这一改进:autocomplete=“新密码”,这将解决这个问题,但我不确定,如果一段时间后Chrome再次将其更改为不同的功能。

这适用于Chrome 84.0.4147.105

1.为密码字段分配文本类型并添加类,例如“fakepasswordtype”

<input type="text" class="fakepasswordtype" name="password1">
<input type="text" class="fakepasswordtype" name="password2">

2.然后在第一次输入完成时,使用jQuery将类型改回密码

jQuery(document).ready(function($) {
    $('.fakepasswordtype').on('input', function(e){
        $(this).prop('type', 'password');
    });
});

这阻止了Chrome在自动填充方面的丑陋行为。

我通过添加两个假输入并赋予它们绝对位置来阻止Chrome 66自动填充:

<form style="position: relative">
  <div style="position: absolute; top: -999px; left: -999px;">
    <input name="username" type="text" />
    <input name="password" type="password" />
  </div>
  <input name="username" type="text" />
  <input name="password" type="password" />

起初,我尝试添加display:none;但Chrome忽略了这些输入,并自动填充可见的输入。

我已经解决了在页面加载后放置此代码的问题:

<script>
var randomicAtomic = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
  $('input[type=text]').attr('autocomplete',randomicAtomic);
</script>

我正在四处寻找解决方案,但没有一个在所有浏览器上都能正常工作。

我尝试了自动完成off、none、nope、new_input_64(甚至是一些有趣的文本),因为autocomplete属性希望传递字符串,无论是什么。

经过多次搜索和尝试,我找到了这个解决方案。

我将所有输入类型都改为文本,并添加了一些简单的CSS代码。

以下是表格:

<form class="row gy-3">
  <div class="col-md-12">
    <label for="fullname" class="form-label">Full Name</label>
    <input type="text" class="form-control" id="fullname" name="fullname" value="">
  </div>
  <div class="col-md-6">
    <label for="username" class="form-label">User Name</label>
    <input type="text" class="form-control" id="username" name="username" value="">
  </div>
  <div class="col-md-6">
    <label for="email" class="form-label">Email</label>
    <input type="text" class="form-control" id="email" name="email" value="">
  </div>
  <div class="col-md-6">
    <label for="password" class="form-label">Password</label>
    <input type="text" class="form-control pswd" id="password" name="password" value="">
  </div>
  <div class="col-md-6">
    <label for="password" class="form-label">Confirm Password</label>
    <input type="text" class="form-control pswd" id="password" name="password" value="">
  </div>
  <div class="col-md-12">
    <label for="recaptcha" class="form-label">ReCaptcha</label>
    <input type="text" class="form-control" id="recaptcha" name="recaptcha" value="">
  </div>
</form>

隐藏密码的CSS代码:

.pswd {
    -webkit-text-security: disc;
}

在Chrome、Opera和Edge上测试。