Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save niksumeiko/360164708c3b326bd1c8 to your computer and use it in GitHub Desktop.
Save niksumeiko/360164708c3b326bd1c8 to your computer and use it in GitHub Desktop.
Disable HTML form input autocomplete and autofill

Disable HTML Form Input Autocomplete and Autofill

  1. Add autocomplete="off" onto <form> element;
  2. Add hidden <input> with autocomplete="false" as a first children element of the form.
<form autocomplete="off" method="post" action="">
    <input autocomplete="false" name="hidden" type="text" style="display:none;">
    ...

This formation is going to prevent Chrome and Firefox to offer autofill and autocomplete for all input fields inside the form.

@moosefaceee
Copy link

Couldn’t agree more @tlevivsoft, would you mind sharing your frustrations with the Chromium team here? https://issues.chromium.org/issues/40910184?pli=1

Better yet, mark yourself as affected. More traction we can get on the ticket the better.

@moosefaceee
Copy link

@rawaby88 you too if you’re open to it please

@GabrielDSousa
Copy link

None of these works for me either.
The only thing I have seen is that when you remove the name AND id, the input text doesn't show suggestions. For me, that's enough, but it isn't sufficient for all uses.

@euCaioSantos
Copy link

euCaioSantos commented Nov 17, 2024

My form is in Wordpress Elementor, I used this HTML code for a long time to stop the autocomplete of the number field.

But as incredible as it may seem, it only works when there are only 2 fields on the form, as soon as I add the email field, the field: name returns to autocomplete.

my code for elementor!

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Função para aplicar configurações ao campo
        function applySettings() {
            let gtmwhatsapp = document.getElementById('form-field-gtmwhatsapp');
            if (gtmwhatsapp) {
                gtmwhatsapp.minLength = 15;
                gtmwhatsapp.maxLength = 15;
                gtmwhatsapp.pattern = "\\(\\d{2}\\) \\d{5}-\\d{4}";
                gtmwhatsapp.title = "Adicione o número corretamente. Ex: (63) 9 9999-9999";

                gtmwhatsapp.addEventListener('input', function () {
                    // Remove todos os caracteres não numéricos
                    gtmwhatsapp.value = gtmwhatsapp.value.replace(/\D/g, '');

                    // Adiciona formatação "(xx) xxxxx-xxxx"
                    if (gtmwhatsapp.value.length >= 11) {
                        gtmwhatsapp.value = gtmwhatsapp.value.replace(/(\d{2})(\d{5})(\d{4})/, "($1) $2-$3");
                    }
                });

                const form = gtmwhatsapp.form;
                if (form) {
                    form.addEventListener('submit', function (event) {
                        if (!gtmwhatsapp.validity.valid) {
                            alert('Adicione o número corretamente');
                            event.preventDefault();
                        }
                    });
                }
                
                gtmwhatsapp.setAttribute('autocomplete', 'off');
                gtmwhatsapp.setAttribute('inputmode', 'numeric'); // Adiciona inputmode como numeric
            }
        }

        // Aplicar configurações ao carregar a página
        applySettings();

        // Evento específico do Elementor para detectar abertura do popup
        jQuery(document).on('elementor/popup/show', function() {
            applySettings();
        });
    });
</script>

@rubens-shoji
Copy link

rubens-shoji commented Nov 22, 2024

This works for me!

The readonly attr with a onfocus function.

<input
readonly onfocus="this.removeAttribute('readonly');"
v-model="form.email"
type="text"
name="email"
id="email"
autocomplete="off"
/>

@upangka
Copy link

upangka commented Dec 27, 2024

  input:-webkit-autofill {
    transition: background-color 5000s ease-in-out 0s;
  }

@LuizAugustoS
Copy link

i'm using AngularJS and have this problem:
I have 4 inputs: Name, ProcessNumber, LicenseNumber and ExpirationDate, in this follow sequence.
if I fill in sequentially, no problem, but, if i fill the ExpirationDate first, the autocomplete="off" don't work. I've tried everything you sent in this trend, but, nothing works.

@zsidil
Copy link

zsidil commented Mar 12, 2025

I found a very simple solution to the problem. I set the readonly attribute on the input fields, which prevents automatic filling. Then I remove the readonly attribute on the onfocus event, when the user clicks on the input field, it becomes writable.
<input name="email" onfocus="this.removeAttribute('readonly')" readonly type="text">

@Jonathanvictor-dev
Copy link

Jonathanvictor-dev commented Mar 13, 2025

readonly

The problem was solved with the solution in the following comment above.

I'm using react, I included readOnly in the inputs, combined it with useEffect, addEventListener, removeEventListener and the 'focus' event. This way, the field is not filled automatically.

@dmbritos
Copy link

dmbritos commented Mar 20, 2025

It also works if you remove the readOnly attribute in the window's load event:

<input type="password" readonly id="MyPasswordInput" name="MyPasswordInput"/>

$(window).load(function () {
    $("#MyPasswordInput").removeAttr('readonly');
});

Just to clarify, NO need to change or add anything to the username/login input...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment