References

Beginner-friendly references for web development, with live, editable examples.

The HTML accept-charset attribute

Attribute All modern browsers
In short

The accept-charset attribute sets a form's submission encoding, and UTF-8 is the only value the HTML standard allows. On a page that is already UTF-8, that value changes nothing. It matters on a page served in an older encoding, where it switches the form's data to UTF-8.

Overview

The HTML accept-charset attribute chooses the character encoding for the text a form submits. UTF-8 is the one encoding name permitted here, and letter case doesn't matter.

The attribute once took several encodings, separated by commas or spaces. Validators now reject any other value, such as ISO-8859-1, with the message that utf-8 is the only allowed value.

The default comes from the page. With no accept-charset, the browser encodes the data in the page's own encoding, so a page with <meta charset="utf-8"> already sends UTF-8 and a windows-1252 page sends windows-1252.

Browsers still act on old values. A form with accept-charset="ISO-8859-1" sends windows-1252, the encoding that the ISO-8859-1 label maps to on the web.

Syntax

<form action="/signup" method="post" accept-charset="UTF-8">
  <label for="city">City</label>
  <input id="city" name="city">
  <button>Sign up</button>
</form>

Values

Value
UTF-8 in any letter case, the only value HTML allows.

Example

Live example
<form id="signup" action="data:text/html;charset=windows-1252,%3Cscript%3Eparent.postMessage(location.search,'*')%3C/script%3E" target="sent" accept-charset="UTF-8">
  <input type="hidden" name="_charset_">
  <label for="city">City</label>
  <input id="city" name="city" value="S&atilde;o Paulo &#10003;"><br>
  <label for="value">accept-charset</label>
  <select id="value">
    <option>UTF-8</option>
    <option>ISO-8859-1</option>
  </select>
  <button>Sign up</button>
</form>
<iframe name="sent" title="Submission target" hidden></iframe>
<p id="out" role="status" style="overflow-wrap:anywhere"></p>
<p>Press Sign up, choose the old value ISO-8859-1 and press it again. The form really submits to a hidden frame, which reports the query it received.</p>
<script>
const form = document.getElementById('signup');
document.getElementById('value').addEventListener('change', function () {
  form.setAttribute('accept-charset', this.value);
});
form.addEventListener('submit', function (e) {
  e.preventDefault();
  const old = document.querySelector('iframe[name="sent"]');
  old.replaceWith(old.cloneNode());
  form.submit();
});
window.addEventListener('message', function (e) {
  if (e.source !== document.querySelector('iframe[name="sent"]').contentWindow) return;
  document.getElementById('out').textContent = 'The form sent ' + e.data.slice(1);
});
</script>

Best practices

  • On a legacy page that can't move to UTF-8 yet, add accept-charset="UTF-8" to each form. Its data then reaches the server as UTF-8, whatever encoding the page itself uses.
  • Write the value as UTF-8 alone. For an old list such as UTF-8, ISO-8859-1, Chrome sends UTF-8, while Firefox splits only on spaces, as the HTML standard does, and sends windows-1252.
  • Include a hidden input named _charset_ with no value when pages in different encodings post to one URL. The browser fills it with the encoding it used, such as UTF-8, so the server knows how to decode the rest.
  • Test a form by submitting a check mark. UTF-8 sends it as %E2%9C%93, while a windows-1252 form sends the text &#10003;, which a server can't tell apart from someone typing those characters.

Frequently asked questions

What value should accept-charset have?
The only value accept-charset may have is UTF-8, and capitals are optional. Any other encoding name, or a list of names, makes the markup invalid.
Do UTF-8 pages need accept-charset?
No, a form on a UTF-8 page already submits UTF-8, so setting accept-charset to UTF-8 changes nothing there. That value only changes the result on a page served in another encoding.
Which encoding is used if accept-charset says UTF-16?
With accept-charset set to UTF-16, browsers send UTF-8 instead. UTF-16 is swapped for UTF-8 when a form's data or a URL is encoded, and UTF-16 isn't a valid value anyway.
Can a submit button change the accept-charset encoding?
No, a button can override a form's action or method, but not its accept-charset. Buttons have formaction, formenctype, formmethod, formnovalidate and formtarget, and none of them sets the character encoding.
How is accept-charset different from enctype?
The accept-charset attribute sets the character encoding, while enctype sets the format, such as multipart/form-data. All three enctype formats encode their text with that encoding.