Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid Characters in Option Value

Tags:

html

option

I was just wondering what valid characters can be included as the value for a <option>

i.e. is this valid?

<select>
    <option value='0dbl,2sgl'>0 Double and 2 Singles</option>
    <option value='1dbl,0sgl'>1 Double and 0 Singles</option>
</select>
like image 279
silent1mezzo Avatar asked May 19 '11 19:05

silent1mezzo


2 Answers

Yes, that's perfectly valid. See the specification: it says the content of value should be CDATA, in which pretty much everything is valid, with the following caveats:

  • Replace character entities with characters,
  • Ignore line feeds,
  • Replace each carriage return or tab with a single space.
like image 151
lonesomeday Avatar answered Sep 24 '22 08:09

lonesomeday


For HTML4:

OPTION Attribute definitions

selected [CI]
  When set, this boolean attribute specifies that this option is pre-selected.
value = cdata [CS]
  This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.
label = text [CS]
  This attribute allows authors to specify a shorter label for an option than the content of the OPTION element. When specified, user agents should use the value of this attribute rather than the content of the OPTION element as the option label.

Source: http://www.w3.org/TR/html401/interact/forms.html#h-17.6

So we go to the definition of CDATA:

CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows:

  • Replace character entities with characters,
  • Ignore line feeds,
  • Replace each carriage return or tab with a single space. User agents may ignore leading and trailing white space in CDATA attribute values (e.g., " myval " may be interpreted as "myval"). Authors should not declare attribute values with leading or trailing white space.

For some HTML 4 attributes with CDATA attribute values, the specification imposes further constraints on the set of legal values for the attribute that may not be expressed by the DTD.

Source: http://www.w3.org/TR/html401/types.html#type-cdata

As there is no constraint noted, the valid content of value must have properly escaped entities, properly defined entities, and be within the scope of the document's encoding.

like image 43
onteria_ Avatar answered Sep 22 '22 08:09

onteria_