16

I want to know what is good practice for select option values.

Example

<select name="select">
  <option value="0-9">Sample</option>
  <option value="a-z">Sample</option>
  <option value="this is sample value">Sample</option>
  <option value="this-is-sample-value">Sample</option>
  <option value="this_is_sample_value">Sample</option>
  <option value="this & is | sample ** value">Sample</option>
</select>

I'm a little bit confused here. Is the select value same like input text and textarea

jdrake
  • 333
  • 4
  • 17
wow
  • 7,989
  • 17
  • 53
  • 63

4 Answers4

21

There are no limits real to the type of data that can be set in the value attribute of the option element. Characters with special meaning in HTML do, of course, need to be represented by the appropriate entities (& as &amp; for example (although the one in the question meets the "followed by a space character" exception to the rule)).

The attribute is defined as containing CDATA:

<!ELEMENT OPTION - O (#PCDATA)         -- selectable choice -->
<!ATTLIST OPTION
  %attrs;                              -- %coreattrs, %i18n, %events --
  selected    (selected)     #IMPLIED
  disabled    (disabled)     #IMPLIED  -- unavailable in this context --
  label       %Text;         #IMPLIED  -- for use in hierarchical menus --
  value       CDATA          #IMPLIED  -- defaults to element content --
  >

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

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.

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

The specification doesn't impose additional limits for the option element's value attribute.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I get problems with comma character – Reign.85 Mar 01 '16 at 10:04
  • @Reign.85 — Then [ask a question](http://stackoverflow.com/questions/ask) about that problem, making sure that you include [a proper test case](http://stackoverflow.com/help/mcve). – Quentin Mar 01 '16 at 10:18
1

Same as a text-type input -- it can be string, float, etc. This is more a question of which is most reliable to parse when you process the form data.

Craig
  • 358
  • 1
  • 9
  • No, it can be CDATA. You might parse it into a string, float, etc on the server, but it isn't in that state in the HTML document. – Quentin Feb 04 '11 at 17:44
  • Right, CDATA in the HTML, but can be parsed in a variety of ways. Semantic issue, I suppose. Thanks for the clarification. – Craig Feb 04 '11 at 18:15
1

The posted value will be the one corresponding to the selection.

In that regards, it is treated the same way as an input type text is.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

Yes, it is a string type, and could have any value. The value goes when you submit a form, and there are limitations.

The limitations depends which technology you are using on server end.

As in case of ASP.Net when you try to post special characters like & or especially < script > some script < / script > or the similar characters which are part of html tags or could be a dangerous script. The asp.net checks the posted data and throws exception. means some special characters are not allowed in value of select box with regards to asp.net

However the samples you given (except of having & it should be prefixed by amp;) are allowed and could be set in option tag value attribute.

Hope your understanding are build.

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38