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


当前回答

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

我尝试了自动完成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上测试。

其他回答

这就是我们所说的文本框自动完成。

我们可以通过两种方式禁用文本框的自动完成:

按浏览器标签按代码要在浏览器中禁用,请转到设置

转到“高级设置”,取消选中复选框,然后单击“还原”。

如果要禁用编码标签,可以执行以下操作-

使用AutoCompleteType=“禁用”:

<asp:TextBox runat="server" ID="txt_userid" AutoCompleteType="Disabled"></asp:TextBox>

通过设置表单自动完成=“off”:

<asp:TextBox runat="server" ID="txt_userid" autocomplete="off"></asp:TextBox>

通过设置表单自动完成=“off”:

<form id="form1" runat="server" autocomplete="off">
    // Your content
</form>

通过使用.cs页面中的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        txt_userid.Attributes.Add("autocomplete", "off");
    }
}

通过使用jQuery

<head runat="server">
    <title></title>

    <script src="Scripts/jquery-1.6.4.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#txt_userid').attr('autocomplete', 'off');
        });
    </script>

我的jQuery解决方案。它可能不是100%可靠,但它对我有用。这个想法在代码注释中描述。

/***防止字段自动填充字段。*当关注具有自动完成功能的文本字段(值为:“off”、“none”、“false”)时,我们将该值替换为新的唯一值(此处为-“off forced-[TIMESTAMP]”),*浏览器在保存的值中找不到这种类型的自动完成,并且不提供选项。*然后,为了防止输入的文本保存在浏览器中,以便进行新的唯一自动完成,当字段失去焦点或用户按下Enter键时,我们将其替换为先前设置的文本。*@type{{init:*}}*/var PreventFieldsAutofill=(函数(){函数init(){events.onPageStart();}var事件={onPageStart:函数(){$(document).on('fous','输入[autocomplete=“off”],输入[autocomplete=“none”],input[autocomplete=“false”]',函数(){methods.replaceAttrs($(this));});$(document).on('blur','input[data prev autocomplete]',函数(){methods.returnAttrs($(this));});$(document).on('keydown','input[data prev autocomplete]',函数(事件){if(event.keyCode==13 | | event.which==13){methods.returnAttrs($(this));}});$(document).on('submit','form',函数(){$(this).find('input[data prev autocomplete]').each(函数(){methods.returnAttrs($(this));});});}};var方法={/***将autocomplete和name属性的值替换为unique,并将原始值保存为新的数据属性*@param$输入*/replaceAttrs:函数($input){var randomString='off-forced-'+Date.now();$input.attr('data-prev-autocomplete',$input.aattr('自动完成'));$input.attr('自动完成',随机字符串);if($input.attr('name')){$input.attr('data-prev-name',$input.atr('name'));$input.attr('name',randomString);}},/***恢复原始自动完成和名称值,以防止在浏览器中保存文本以获取唯一值*@param$输入*/returnAttrs:函数($input){$input.attr('自动完成',$input.atr('数据-版本-自动完成'));$input.removeAttr('data-prev-autocomplete');if($input.attr('data-prev-name')){$input.attr('name',$input.atr('data-prev-name'));$input.removeAttr('data-prev-name');}}};返回{init:初始化}})();PreventFieldsAutofill.init();.输入{显示:块;宽度:90%;填充:6px 12px;字体大小:14px;线高:1.42857143;颜色:#555555;背景色:#fff;背景图像:无;边框:1px实心#ccc;边界半径:4px;}<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js“></script><form action=“#”><p><label for=“input-1”>Firts name without autocomplete</label><br/><input id=“input-1”class=“input”type=“text”name=“first name”autocomplete=“off”placeholder=“Firts name without autocomplete”/></p><p><label for=“input-2”>Firts name with autocomplete</label><br/><input id=“input-2”class=“input”type=“text”name=“first name”autocomplete=“given name”placeholder=“Firts name with autocomplete”/></p><p><button type=“submit”>提交表单</button></p></form>

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

我尝试了自动完成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上测试。

这是浏览器现在忽略的安全问题。浏览器使用输入名称识别和存储内容,即使开发人员认为信息是敏感的,不应存储。

在两个请求之间设置不同的输入名称将解决问题(但仍将保存在浏览器的缓存中,并且还会增加浏览器的缓存)。

要求用户激活或停用浏览器设置中的选项不是一个好的解决方案。该问题可以在后端解决。

这是解决办法。所有自动完成的元素都是使用如下隐藏输入生成的:

<?php $r = md5(rand() . microtime(TRUE)); ?>
<form method="POST" action="./">
    <input type="text" name="<?php echo $r; ?>" />
    <input type="hidden" name="__autocomplete_fix_<?php echo $r; ?>" value="username" />
    <input type="submit" name="submit" value="submit" />
</form>

然后,服务器像这样处理后置变量:(Demo)

foreach ($_POST as $key => $val) {
    $newKey = preg_replace('~^__autocomplete_fix_~', '', $key, 1, $count);
    if ($count) {
        $_POST[$val] = $_POST[$newKey];
        unset($_POST[$key], $_POST[$newKey]);
    }
}

可以照常访问该值

echo $_POST['username'];

而且浏览器将无法从以前的请求或以前的用户那里建议信息。

即使浏览器更新其技术以忽略/尊重自动完成属性,这将继续有效。

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

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

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