<form autocomplete="off"><!-- These inputs will not allow autocomplete and Chromewon't highlight them yellow! --><input name="username" /><input name="password" type="password" /><!-- This field will allow autocomplete to work eventhough we've disabled it on the form --><input name="another_field" autocomplete="on" /></form>
<input id="email" readonly type="email" onfocus="if (this.hasAttribute('readonly')) {this.removeAttribute('readonly');// fix for mobile safari to show virtual keyboardthis.blur(); this.focus(); }" />
<!-- This is a fake password input to defeat the browser's autofill behavior --><input type="password" id="txtPassword" style="display:none;" /><!-- This is the real password input --><input type="password" id="txtThisIsTheRealPassword" />
// Prevent input autocomplete$.fn.preventAutocomplete = function() {this.each(function () {var $el = $(this);$el.clone(false, false) // Make a copy (except events).insertBefore($el) // Place it before original field.prop('id', '') // Prevent ID duplicates.hide() // Make it invisible for user;});};
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);}}
/*** Prevent fields autofill for fields.* When focusing on a text field with autocomplete (with values: "off", "none", "false") we replace the value with a new and unique one (here it is - "off-forced-[TIMESTAMP]"),* the browser does not find this type of autocomplete in the saved values and does not offer options.* Then, to prevent the entered text from being saved in the browser for a our new unique autocomplete, we replace it with the one set earlier when the field loses focus or when user press Enter key.* @type \{\{init: *}}*/var PreventFieldsAutofill = (function () {function init () {events.onPageStart();}
var events = {onPageStart: function () {$(document).on('focus', 'input[autocomplete="off"], input[autocomplete="none"], input[autocomplete="false"]', function () {methods.replaceAttrs($(this));});$(document).on('blur', 'input[data-prev-autocomplete]', function () {methods.returnAttrs($(this));});$(document).on('keydown', 'input[data-prev-autocomplete]', function (event) {if (event.keyCode == 13 || event.which == 13) {methods.returnAttrs($(this));}});$(document).on('submit', 'form', function () {$(this).find('input[data-prev-autocomplete]').each(function () {methods.returnAttrs($(this));});});}};
var methods = {/*** Replace value of autocomplete and name attribute for unique and save the original value to new data attributes* @param $input*/replaceAttrs: function ($input) {var randomString = 'off-forced-' + Date.now();$input.attr('data-prev-autocomplete', $input.attr('autocomplete'));$input.attr('autocomplete', randomString);if ($input.attr('name')) {$input.attr('data-prev-name', $input.attr('name'));$input.attr('name', randomString);}},/*** Restore original autocomplete and name value for prevent saving text in browser for unique value* @param $input*/returnAttrs: function ($input) {$input.attr('autocomplete', $input.attr('data-prev-autocomplete'));$input.removeAttr('data-prev-autocomplete');if ($input.attr('data-prev-name')) {$input.attr('name', $input.attr('data-prev-name'));$input.removeAttr('data-prev-name');}}};
return {init: init}})();PreventFieldsAutofill.init();
<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">Submit form</button></p></form>
function protectMe(el,action){
// Remove the white space we injected on startupvar v = el.value.trim();
// Ignore this reset process if we are typing content in// and only acknowledge a keypress when it's the last Delete// we do resulting in the form value being emptyif(action=='ku' && v != ''){return;}
// Fix double quote issue (in the form input) that results from writing the outerHTML back to itselfv = v.replace(/"/g,'\\"');
// Reset the popup appearing when the form came into focus from a click by rewriting it backel.outerHTML=el.outerHTML;
// Issue instruction to refocus it again, insert the value that existed before we reset it and then select the content.setTimeout("var d=document.getElementById('"+el.id+"'); d.value=\""+v+"\";d.focus();if(d.value.length>1){d.select();}",100);}
set the autocomplete attribute of the password input element to "new-password"
<form autocomplete="off">....other element<input type="password" autocomplete="new-password"/></form>
对于用户名和密码输入类型字段,有一些有限的浏览器支持autocomplete触发“关闭”功能的更具体的属性。用于登录的文本输入上的autocomplete="username"将强制某些浏览器重置并删除自动完成功能。autocomplete="new-password"将尝试使用“密码”类型输入字段对密码做同样的事情。(见下文)。然而,这些是特定浏览器的专有,并且在大多数浏览器中都会失败。不支持这些功能的浏览器包括Internet Explorer 1-11,许多Safari和Opera桌面浏览器,以及某些版本的iPhone和Android默认浏览器。但它们提供了额外的功能来尝试和强制删除自动完成。
<!-- input without the "name" or "id" --><input type="text" oninput="this.nextElementSibling.value=this.value"><input type="hidden" name="email" id="email">