-- online Search!

Disable Form Autocompletion

by kirupa   |    filed under HTML and CSS

Learn how to request different form autocompletion behavior and where the browser still has the final say.

One of the greatest inventions since sliced bread is our browser's auto completion behavior. We focus on a field like input, select, or textarea and see values we can select instead of writing those values manually again...like an animal:

I don’t know about you, but this simple act of our browsers remembering commonly provided information is a major timesaver. It has made the boring act of typing and re-typing names, addresses, phone numbers, and more a non-activity! Now, there will be times when the form autocomplete/autofill behavior isn’t quite what we want. Some examples of such times are when we are providing:

  1. A one-time code that should be identified as a new code instead of an old saved password
  2. Something really sensitive like a government-issued ID value
  3. A trivial/repetitive value that like some words/numbers as part of an online calculator, word game, etc.

There are probably a boatload more examples, but the important detail is that we as developers may want to disable the autocompletion behavior our browsers provide by default. This article will show us how.

Onwards!

Meet the Autocomplete Attribute

We can give the browser guidance with the autocomplete attribute. A value of on allows autocompletion, while off requests that it not be used. A field without its own value normally uses its owning form's setting. This is a hint, not a security boundary: browsers and password managers may still offer to save or fill login credentials. It does not encrypt a value, validate it, or guarantee that nobody stores it.

The following example requests that autocomplete be disabled for the fields in an entire form:

<form autocomplete="off" id="sumacForm">
  <input type="number" min="1" required id="firstNumber">
  <input type="number" min="1" required id="secondNumber">
  <button type="submit">Calculate</button>
</form>

If one field needs different guidance, we can set autocomplete on that field instead of turning it off for the whole form. For a one-time authentication code, one-time-code tells a supporting browser what kind of value we expect:

<form id="infoForm">
  <input type="text" placeholder="username" required>
  <input type="password" placeholder="password" required>
  <input type="text" inputmode="numeric" autocomplete="one-time-code" placeholder="2FA code" required>
  <button type="submit">Submit</button>
</form>

All of this isn't just limited to HTML. If you need to change the autocomplete behavior via JavaScript, you can use setAttribute(see tutorial) on the form or input element as shown in the following snippet:

<form id="infoForm">
  <input type="text" placeholder="username" required>
  <input type="password" placeholder="password" required>
  <input id="twoFA" type="text" inputmode="numeric" placeholder="2FA code" required>
  <button type="submit">Submit</button>
</form>

<script>
  let twoFA = document.querySelector("#twoFA");
  twoFA.setAttribute("autocomplete", "one-time-code");
</script>

Whether HTML or JavaScript is your jam, we are giving the browser the same field-level guidance. Our code is an identifier, so the example keeps it as text and uses inputmode="numeric" as a keyboard hint. This avoids treating a leading zero as something to discard during numeric conversion. The server still needs to validate the code, and we should test the experience with the browsers and password managers our users actually use.

Conclusion

What we have seen are ways to guide autocompletion for a whole form or an individual field. Values such as name, username, email, and one-time-code help the browser distinguish the information we want. We don't need to make people retype everything just because one field needs different treatment. This MDN article explains the available values and the limits of this guidance.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--