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


当前回答

为了解决这个问题,我使用了一些CSS技巧和以下工作。

input {
    text-security:disc;
    -webkit-text-security:disc;
    -mox-text-security:disc;
}

请阅读本文了解更多详情。

其他回答

我已经在这场永无止境的战斗中战斗了很久。。。所有的技巧和黑客最终都会停止工作,就像浏览器开发人员正在阅读这个问题一样。

我不想随机化域名,不想修改服务器端代码,不想使用JS重磅技巧,也不想将“黑客攻击”降至最低。下面是我想到的:

TL;DR使用完全没有名称或id的输入!并跟踪隐藏字段中的更改

<!-- input without the "name" or "id" -->
<input type="text" oninput="this.nextElementSibling.value=this.value">
<input type="hidden" name="email" id="email">

显然,可以在所有主要浏览器中使用。

P.S.已知的小问题:

您不能再通过id或名称引用此字段。但您可以使用CSS类。或者使用$(#email').prev();在jQuery中。或者想出另一个解决方案(有很多)。当以编程方式更改文本框值时,oninput和onchange事件不会触发。因此,相应地修改代码以反映隐藏字段中的更改。

要防止浏览器自动填充用户保存的站点登录凭据,请在表单顶部放置一个文本和密码输入字段,该字段具有非空值,并设置样式“position:aabsolute;top:-999px;left:-999px”以隐藏字段。

<form>
  <input type="text" name="username_X" value="-" tabindex="-1" aria-hidden="true" style="position: absolute; top: -999px; left:-999px" />
  <input type="password" name="password_X" value="-" tabindex="-1" aria-hidden="true" style="position: absolute; top: -999px; left:-999px" />
  <!-- Place the form elements below here. -->
</form>

在密码字段之前有一个文本字段很重要。否则,在某些情况下可能无法阻止自动填充。

重要的是文本和密码字段的值不能为空,以防止在某些情况下覆盖默认值。

这两个字段必须在表单中的“真实”密码类型字段之前。

对于兼容html 5.3的较新浏览器,自动完成属性值“新密码”应该有效。

<form>
  <input type="text" name="username" value="" />
  <input type="password" name="password" value="" autocomplete="new-password" />
</form>

这两种方法的组合可用于支持较旧和较新的浏览器。

<form>
  <div style="display:none">
    <input type="text" readonly tabindex="-1" />
    <input type="password" readonly tabindex="-1" />
  </div>
  <!-- Place the form elements below here. -->
  <input type="text" name="username" value="" />
  <input type="password" name="password" value="" autocomplete="new-password" />
</form>

2022年9月

一些浏览器,如Google Chrome,甚至不关心autocomplete属性的值。停止获取建议的最佳方法是将输入的name属性更改为随时间变化的属性

截至2022年9月,浏览器将不提供一次性密码自动完成功能

所以我们可以使用otp作为域名。

<input name=“otp”>

这对我来说就像一种魅力。

将表单的自动完成属性设置为关闭添加虚拟输入字段,并将其属性设置为禁用。

<form autocomplete=“off”><input-type=“text”autocomplete=“off”style=“display:none”></form>

Chrome计划支持这一点。

目前,最好的建议是使用很少自动完成的输入类型。

Chrome讨论

<input type='search' name="whatever" />

要与Firefox兼容,请使用normalautocomplete='off'

<input type='search' name="whatever" autocomplete='off' />