如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?
当前回答
我使用此TextMode=“password”autocomplete=“new password”,并在aspx txtPassword.Attributes.Add(“value”,“”)中加载页面;
其他回答
我正在四处寻找解决方案,但没有一个在所有浏览器上都能正常工作。
我尝试了自动完成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上测试。
如果不使用客户端和服务器端代码的组合,似乎不可能实现这一点。
为了确保用户每次都必须填写表单而不必自动完成,我使用了以下技术:
在服务器上生成表单字段名称,并使用隐藏的输入字段来存储这些名称,以便在提交到服务器时,服务器端代码可以使用生成的名称来访问字段值。这是为了阻止用户选择自动填充字段。在表单上放置每个表单字段的三个实例,并使用css隐藏每个集合的第一个和最后一个字段,然后在使用javascript加载页面后禁用它们。这是为了防止浏览器自动填写字段。
这里有一个fiddle演示了javascript、css和html,如#2所述https://jsfiddle.net/xnbxbpv4/
javascript代码:
$(document).ready(function() {
$(".disable-input").attr("disabled", "disabled");
});
css:
.disable-input {
display: none;
}
html格式:
<form>
<input type="email" name="username" placeholder="username" class="disable-input">
<input type="email" name="username" placeholder="username">
<input type="email" name="username" placeholder="username" class="disable-input">
<br>
<input type="password" name="password" placeholder="password" class="disable-input">
<input type="password" name="password" placeholder="password">
<input type="password" name="password" placeholder="password" class="disable-input">
<br>
<input type="submit" value="submit">
</form>
下面是一个使用asp.net和razor的服务器代码的粗略示例
型号:
public class FormModel
{
public string Username { get; set; }
public string Password { get; set; }
}
控制器:
public class FormController : Controller
{
public ActionResult Form()
{
var m = new FormModel();
m.Username = "F" + Guid.NewGuid().ToString();
m.Password = "F" + Guid.NewGuid().ToString();
return View(m);
}
public ActionResult Form(FormModel m)
{
var u = Request.Form[m.Username];
var p = Request.Form[m.Password];
// todo: do something with the form values
...
return View(m);
}
}
视图:
@model FormModel
@using (Html.BeginForm("Form", "Form"))
{
@Html.HiddenFor(m => m.UserName)
@Html.HiddenFor(m => m.Password)
<input type="email" name="@Model.Username" placeholder="username" class="disable-input">
<input type="email" name="@Model.Username" placeholder="username">
<input type="email" name="@Model.Username" placeholder="username" class="disable-input">
<br>
<input type="password" name="@Model.Password" placeholder="password" class="disable-input">
<input type="password" name="@Model.Password" placeholder="password">
<input type="password" name="@Model.Password" placeholder="password" class="disable-input">
<br>
<input type="submit" value="submit">
}
对于React,您可以尝试将此代码放在表单下面或密码输入上面,或者放在电子邮件和密码输入之间
export const HackRemoveBrowsersAutofill = () => (
<>
<input type="email" autoComplete="new-password" style={ { display: 'none' } } />
<input type="password" autoComplete="new-password" style={ { display: 'none' } } />
</>
)
示例之一:
<input type="email"/>
<HackRemoveBrowsersAutofill/>
<input type="password"/>
只需设置autocomplete=“off”。这样做有一个很好的理由:您希望提供自己的自动完成功能!
很确定这是目前最通用的解决方案
这将停止自动完成和弹出的建议框。
简介
让我们面对现实,自动完成=关闭或新密码似乎不起作用。这是应该做的,但却没有,每周我们都会发现其他事情发生了变化,浏览器会用我们不想要的更多随机垃圾填写表单。我发现了一个非常简单的解决方案,它不需要在登录页面上自动完成。
如何实施
步骤1)。将相同的函数调用添加到用户名和密码字段的onmousedown和onkeyup属性,确保为它们提供一个id,并在函数调用结束时记录代码。md=mousedown和ku=keyup
对于用户名字段,仅添加值;因为这防止了表单在进入页面时自动填充。
例如:
<input type=“text”value=“ ;”id=“myusername”onmousedown=“protectMe(this,'md')”onkeyup=“protectMe(this,'ku')”/>
步骤2)。在页面上包含此功能
功能保护Me(el,action){//删除启动时注入的空白var v=el.value.trim();//如果我们正在键入内容,请忽略此重置过程//并且只在最后一次删除时确认按键//我们这样做会导致表单值为空如果(action=='ku'&&v!=''){return;}//修复了将outerHTML写回自身所导致的双引号问题(在表单输入中)v=v.replace(/“/g,'\\”');//通过重新编写表单,重置单击表单进入焦点时出现的弹出窗口el.outerHTML=el.outrHTML;//发出指令重新调整焦点,插入重置之前存在的值,然后选择内容。setTimeout(“var d=document.getElementById('“+el.id+”');d.value=\“”+v+“\”;d.focus();if(d.value.length>1){d.select();}”,100);}
它做什么?
首先,它添加了;字段的空格,以便浏览器尝试查找与此相关的详细信息,但未找到任何内容,因此自动完成固定其次,当您单击时,将再次创建HTML,取消弹出窗口,然后重新选择值。最后,当您使用delete按钮删除字符串时,弹出窗口通常会再次出现,但如果我们达到该点,则keyup检查会重复该过程。
问题是什么?
点击选择一个字符是一个问题,但对于登录页面,大多数人会原谅它选择了所有文本Javascript确实会删除任何空格的输入,但为了安全起见,您可能也希望在服务器端这样做
能更好吗?
可能是的。这只是我测试过的东西,它满足了我的基本需求,但可能还有更多的技巧需要添加,或者有更好的方法来应用所有这些。
已测试的浏览器
截至本发布日期,已在最新的Chrome、Firefox和Edge上测试和工作
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击