Make page to tell browser not to cache/preserve input values

HtmlBrowserFormsBrowser Cache

Html Problem Overview


Most browsers cache form input values. So when the user refreshes a page, the inputs have the same values.

Here's my problem. When a user clicks Save, the server validates POSTed data (e.g. checked products), and if not valid, sends it back to the browser. However, as stated above, even if the server clears selection for some values, they may still be selected because of the browser cache!

My data has invisible (until parent item selected) checkboxes, so the user may be even not aware that some previous value is still selected, until clicking Save again and gets an error message - even though the user thinks it's not. Which is irritating.

This can be resolved by doing Ctrl + F5, but it's not even a solution. Is there an automatic/programmatic way to tell browser not to cache form input data on some form/page?

Html Solutions


Solution 1 - Html

Are you explicitly setting the values as blank? For example:

<input type="text" name="textfield" value="">

That should stop browsers putting data in where it shouldn't. Alternatively, you can add the autocomplete attribute to the form tag:

<form autocomplete="off" ...></form>

Solution 2 - Html

From a Stack Overflow reference

It did not work with value="" if the browser already saves the value so you should add.

For an input tag there's the attribute autocomplete you can set:

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

You can use autocomplete for a form too.

Solution 3 - Html

Another approach would be to reset the form using JavaScript right after the form in the HTML:

<form id="myForm">
  <input type="text" value="" name="myTextInput" />
</form>

<script type="text/javascript">
  document.getElementById("myForm").reset();
</script>

Solution 4 - Html

Basically, there are two ways to clear the cache:

<form autocomplete="off"></form>

or

$('#Textfiledid').attr('autocomplete', 'off');

Solution 5 - Html

This worked for me in newer browsers:

autocomplete="new-password"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionqueen3View Question on Stackoverflow
Solution 1 - HtmlDisgruntledGoatView Answer on Stackoverflow
Solution 2 - HtmlshareefView Answer on Stackoverflow
Solution 3 - HtmlpmkoView Answer on Stackoverflow
Solution 4 - HtmlAnand DwivediView Answer on Stackoverflow
Solution 5 - HtmlerikruniaView Answer on Stackoverflow