121

Is it possible to use CSS to disable autocomplete on a form element (specifically a textfield)?

I use a tag library which does not permit the autocomplete element and I would like to disable autocomplete without using Javascript.

Wex
  • 15,539
  • 10
  • 64
  • 107
David
  • 3,655
  • 7
  • 27
  • 26

11 Answers11

150

As it stands, there is no 'autocomplete off' attribute in CSS. However, html has an easy code for this:

<input type="text" id="foo" value="bar" autocomplete="off" />

If you're looking for a site-wide effector, an easy one would be to simply have a js function to run through all 'input' s and add this tag, or look for the corresponding css class / id.

The autocomplete attribute works fine in Chrome and Firefox (!), but see also Is there a W3C valid way to disable autocomplete in a HTML form?

Community
  • 1
  • 1
RobertsonM
  • 1,519
  • 2
  • 8
  • 2
  • 4
    https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion suggests `autocomplete="anyrandominvalidvalue"` will work. – Andrew Stalker Apr 24 '18 at 08:24
61

You can use a generated id and name everytime, which is different, so the browser cannot remember this text-field and will fail to suggest some values.

This is at least the cross browser safe alternative, but I would recommend to go with the answer from RobertsonM (autocomplete="off").

Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
  • 6
    A side effect of this is, that the browser history gets cluttered with many different (random) form fields. Every lookup for known values in the browsers local databae will take some time. – dermatthias May 27 '11 at 05:25
  • 4
    Nice idea, but it does not prevent credit card numbers from being saved into the unencrypted autocomplete database. – Dr. Hans-Peter Störr Jun 25 '12 at 14:03
  • If you don't use static control names/id's then any scripting will be broken and any form data collection will also break (unless every aspect of your application is aware of the new naming convention and is dynamically updating any scripting/data collection as well). – Gary Richter Apr 08 '15 at 13:50
  • I you must avoid autocomplete, "randomizing" the field names is the only real way to go. Using any hint like `autocomplete="off"` is going to be ignored in Browser X version (current + 1). (For example, latest Google Chrome ignore autocomplete=off.) – Mikko Rantalainen Jan 09 '17 at 11:36
  • @kmoser: Even if you defeat browser's autocomplete feature by randomizing element `id` it will still save it in cache (i.e. on a user's hard drive.) Which is a horrible thing to do to someone's credit card number or any other sensitive information. Additionally, it unnecessarily clutters your user's disk drives. Each time they visit your page a new (cookie-like) file will be saved on their computer. Just for the heck of it, try to clear your Chrome cache and see how much space it will free up (if you haven't done it for a while). That's why I call it an awful idea. – c00000fd Jul 04 '17 at 22:49
  • @c00000fd Thanks for the explanation. I agree that saving someone's credit card number is bad thing, but hard drive clutter is really not an issue in this case since your browser stores other files in its cache (pages, images, etc.) that take orders of magnitude more data. What's more, all browsers give you a way to limit the cache size. – kmoser Jul 05 '17 at 00:54
54

you can easily implement by jQuery

$('input').attr('autocomplete','off');
ahhmarr
  • 2,296
  • 4
  • 27
  • 30
  • 6
    In majority of cases, $('form').attr('autocomplete','off'); is much more efficient (see comment to an answer below) – irakli Feb 28 '14 at 00:26
  • @irakli In my case, using `form` is the ONLY thing that worked. Not `input`. Thanks. – khaverim Jul 22 '14 at 20:20
  • 1
    Hi, 2018 and this is just about the only way I can get this to work these days. Also, @irakli this is true if you need to use on an entire form us the form as the selector. – Devon Kiss Jul 04 '18 at 23:27
  • A javascript version? Or vue version of this? – kirtan403 May 16 '21 at 10:39
18

If you're using a form you can disable all the autocompletes with,

<form id="Form1" runat="server" autocomplete="off">
Ernest
  • 1,370
  • 13
  • 23
  • 1
    indeed, per Mozilla:"If the autocomplete attribute is not specified on an input element, then the browser uses the autocomplete attribute value of the element's form owner. The form owner is either the form element that this element is a descendant of or the form element whose id is specified by the form attribute of the input element. For more information, see the autocomplete attribute in
    ." considering this a much more efficiet jQuery alternative to the one shown above would be: $('form').attr('autocomplete','off');
    – irakli Feb 28 '14 at 00:25
13

CSS does not have this ability. You would need to use client-side scripting.

Sampson
  • 265,109
  • 74
  • 539
  • 565
4

I tried all suggested ways from this question answers and other articles in the web but not working anyway. I tried autocomplete="new-random-value", autocomplete="off" in form element, using client-side script as below but outside of $(document).ready() as one of the user mentioned:

$(':input').on('focus', function () {
  $(this).attr('autocomplete', 'off')
});

I found maybe another priority in the browser cause this weird behavior! So I searched more and finally, I read again carefully below lines from this good article:

For this reason, many modern browsers do not support autocomplete="off" for login fields:

If a site sets autocomplete="off" for a , and the form includes username and password input fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits the page. If a site sets autocomplete="off" for username and password fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits the page. This is the behavior in Firefox (since version 38), Google Chrome (since 34), and Internet Explorer (since version 11).

If you are defining a user management page where a user can specify a new password for another person, and therefore you want to prevent auto-filling of password fields, you can use autocomplete="new-password"; however, support for this value has not been implemented on Firefox.

It's just worked. I tried in chrome specially and I hope this continues working and help others.

QMaster
  • 3,743
  • 3
  • 43
  • 56
3

There are 3 ways to that, i mention them in order of being the better way...

1-HTML way:

<input type="text" autocomplete="off" />

2-Javascript way:

document.getElementById("input-id").getAttribute("autocomplete") = "off";

3-jQuery way:

$('input').attr('autocomplete','off');
sina
  • 2,103
  • 1
  • 19
  • 26
3

I just use 'new-password' instead 'off' on autocomplete.

and I also have try using this code and works (at least on my end), I use WP and GravityForm for your information

$('input').attr('autocomplete','new-password');
2

I solved the problem by adding an fake autocomplete name for all inputs.

$("input").attr("autocomplete", "fake-name-disable-autofill");
Webdma
  • 714
  • 6
  • 16
0

Thanks to @ahhmarr's solution I was able to solve the same problem in my Angular+ui-router environment, which I'll share here for whoever's interested.

In my index.html I've added the following script:

<script type="text/javascript">
    setTimeout(function() {
        $('input').attr('autocomplete', 'off');
    }, 2000);
</script>

Then to cover state changes, I've added the following in my root controller:

$rootScope.$on('$stateChangeStart', function() {
                $timeout(function () {
                    $('input').attr('autocomplete', 'off');
                }, 2000);
            });

The timeouts are for the html to render before applying the jquery.

If you find a better solution please let me know.

Community
  • 1
  • 1
TrampGuy
  • 1,737
  • 1
  • 13
  • 9
0

You can't use CSS to disable autocomplete, but you can use HTML:

<input type="text" autocomplete="false" />

Technically you can replace false with any invalid value and it'll work. This iOS the only solution I've found which also works in Edge.

Andrew
  • 3,335
  • 5
  • 36
  • 45