Now that we've discussed what all of the game elements are and how each HTML5 feature was used to fulfill that role, let's take a deeper look at how to make the most out of each of these APIs. For each of the following APIs, we'll provide a more concrete definition of the feature, what its intended use is, and a code example will follow. You may also refer to the complete source code attached at the end of the chapter in order to fill the gap between the code sample and how that feature fits in with the rest of the game code base. It is also recommended that you code along and play with the various settings and values in order to experiment with and more fully understand each API.
The new HTML5 web forms API adds 13 new input types that allows for a much more flexible and powerful experience. What's more, web forms are also able to validate themselves requiring zero JavaScript intervention.
The following are the new input types defined in the new web forms chapter of the HTML5 specification:
The date input type allows the user to select a specific date from a browser-supplied calendar. The specific format and styling of this calendar is unique to the browser and platform used. The data that results from a date selection is of the form YYYY-MM-DD
.
<input type="date" min="1935-12-16" max="2013-08-19" />
The optional attributes min
and max
can be used to force validation of the date selected by the user to be within a given range. Other valid attributes for the date
input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attributedisabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive controlautocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user, so inputting a stored value in the future can be automatically completed by the browser upon hint by userautofocus
(acceptable values include autofocus
, "", or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loadingmin
(value must be a valid date in the form of "yyyy-mm-dd"): Specifies the lowest date allowed to be selected by the usermax
(value must be a valid date in the form of "yyyy-mm-dd"): Specifies the highest date allowed to be selected by the userreadonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the userstep
(acceptable values include any
or any positive integer): Specifies how the value attribute of the element is to changerequired
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submitvalue
(value must be a valid date in the form of "yyyy-mm-dd"): Specifies the actual date represented by this elementThe month input type allows the user to select a specific month and year from a browser-supplied calendar. The specific format and styling of this calendar is unique to the browser and platform used. The data that results from a date selection is of the form YYYY-MM
.
<input type="month" min="1935-12" max="2013-08" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attributedisabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive control.autocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by userautofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loadingmin
(value must be a valid date in the form of "yyyy-mm"): Specifies the lowest date allowed to be selected by the usermax
(value must be a valid date in the form of "yyyy-mm"): Specifies the highest date allowed to be selected by the userreadonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the userstep
(acceptable values include any
or any positive integer): Specifies how the value attribute of the element is to changerequired
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submitvalue
(value must be a valid date in the form of "yyyy-mm"): Specifies the actual date represented by this elementThe week input type allows the user to select a specific week of a year from a browser-supplied calendar. The specific format and styling of this calendar is unique to the browser and platform used. The data that results from a date selection is of the form YYYY-Www
(for example, 2013-W05).
<input type="week" min="1935-W51" max="2013-W34" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attributedisabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive controlautocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by userautofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loadingmin
(value must be a valid date in the form of "yyyy-Www", where "ww" must be a two digit representation of the week number): Specifies the lowest date allowed to be selected by the usermax
(value must be a valid date in the form of "yyyy-Www", where "ww" must be a two digit representation of the week number): Specifies the highest date allowed to be selected by the userreadonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the userstep
(acceptable values include any
or any positive integer): Specifies how the value attribute of the element is to changerequired
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submitvalue
(value must be a valid date in the form of "yyyy-Www", where "ww" must be a two digit representation of the week number): Specifies the actual date represented by this elementThe time input type allows the user to select a specific time of day. The data in this element is of the form HH:MM:SS.Ms
.
<input type="time" min="16:23:42.108" max="23:59:59.999" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attributedisabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive controlautocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by userautofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loadingmin
(value must be a valid partial time in the form of "HH:MM:SS.Mss", "HH:MM:SS", or "HH:MM"): Specifies the lowest date allowed to be selected by the usermax
(value must be a valid partial time in the form of "HH:MM:SS.Mss", "HH:MM:SS", or "HH:MM"): Specifies the highest date allowed to be selected by the userreadonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the userstep
(acceptable values include any
or any positive integer): Specifies how the value attribute of the element is to changerequired
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submitvalue
(value must be a valid partial time in the form of "HH:MM:SS.Mss", "HH:MM:SS", or "HH:MM"): Specifies the actual date represented by this elementThe datetime input type allows the user to select a specific date and time (including time zone) from a browser-supplied calendar. The specific format and styling of this calendar is unique to the browser and platform used. The data that results from a date selection is of the form YYYY-MM-DDTHH:MM:SS-UTC
.
<input type="datetime" min="1935-12-16T16:23:42-08:00" max="2013-08-19T23:59:59-09:00" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attributedisabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive controlautocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by userautofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loadingmin
(value must be a valid date time, as defined in the RFC 3339): Specifies the lowest date allowed to be selected by the usermax
(value must be a valid date time, as defined in the RFC 3339): Specifies the highest date allowed to be selected by the userreadonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the userstep
(acceptable values include any
or any positive integer): Specifies how the value attribute of the element is to changerequired
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submitvalue
(value must be a valid date time, as defined in the RFC 3339): Specifies the actual date represented by this elementThe datetime-local input type allows the user to select a specific date and time (not including time zone) from a browser-supplied calendar. The specific format and styling of this calendar is unique to the browser and platform used. The data that results from a date selection is of the form YYYY-MM-DDTHH:MM:SS
.
<input type="datetime-local" min="1935-12-16T16:23:42" max="2013-08-19T23:59:59" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attributedisabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive controlautocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by userautofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loadingmin
(value must be a valid partial time in the form of "YYYY-MM-DDTHH:MM:SS.Mss", "YYYY-MM-DDTHH:MM:SS", or "YYYY-MM-DDTHH:MM"): Specifies the lowest date allowed to be selected by the usermax
(value must be a valid partial time in the form of "YYYY-MM-DDTHH:MM:SS.Mss", "YYYY-MM-DDTHH:MM:SS", or "YYYY-MM-DDTHH:MM"): Specifies the highest date allowed to be selected by the userreadonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the userstep
(acceptable values include any
or any positive integer): Specifies how the value attribute of the element is to changerequired
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submitvalue
(value must be a valid partial time in the form of "YYYY-MM-DDTHH:MM:SS.Mss", "YYYY-MM-DDTHH:MM:SS", or "YYYY-MM-DDTHH:MM"): Specifies the actual date represented by this elementThe color input type allows the user to select a specific color from a browser-supplied color picker. The specific format and styling of this color picker widget is unique to the browser and platform used. Although some implementations of the widget may provide values in different format (RGB or HSL), the data that results from a color selection is a hexadecimal representation of the color in the form #RRGGBB
.
<input type="color" value="#900CC1" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attribute.disabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive control.autocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by user.autofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loading.value
(value must be a valid hexadecimal color with exactly seven characters in length and of the form "#rrggbb" or "#RRGGBB"): Specifies the actual color represented by this element. Keywords, such as Color, are not allowed.The email input type allows the user to input an e-mail address. In mobile devices where a digital keyboard is provided for data entry, this input type hints to the system that the keyboard to be provided should be the most appropriate for entering an e-mail address.
<input type="email" placeholder="Enter an email address" pattern="w{3,}@packtpub.com" maxlength="23" />
Other valid attributes for the date input type include the following:
name
(value must be a string): Identifies a particular field by the string value associated with the attribute.disabled
(acceptable values include disabled
, ""
, or empty): Specifies that the element is disabled and cannot receive control.autocomplete
(acceptable values include on
or off
): Specifies whether the browser should store values entered by the user so inputting a stored value in the future can be automatically completed by the browser upon hint by user.autofocus
(acceptable values include autofocus
, ""
, or empty): Specifies to the browser that the element must receive focus immediately after the document finishes loading.maxlength
(value must be a non-negative integer): Specifies the maximum length of characters that the element can contain.pattern
(value must be a valid regular expression pattern as defined by ECMA 262): Specifies a pattern that the browser must validate the specified input against.size
(value must be a positive integer): Specifies the maximum number of characters displayed by the element, although more characters may be allowed to be entered.placeholder
(value must be a string): Specifies a string to be shown to the user as a hint as to what information the field expects. This string disappears when data is entered into the field and is shown when the field becomes empty.multiple
(acceptable values include multiple
, ""
, or empty): Specifies that multiple values are allowed in this element.readonly
(acceptable values include readonly
, ""
, or empty): Specifies that the value of this element cannot be changed by the user.required
(acceptable values include required
, ""
, or empty): Specifies that this element must have a valid value in order for the form to be allowed to submit.value
(value must be a valid e-mail address and must adhere to any further restrictions specified by the pattern attribute, if any): Specifies the actual e-mail address represented by this element or a comma-separated list of valid e-mail addresses when the multiple attribute is present.For those not familiar with JavaScript's regular expression language, or who need a refresher, following is a summary of the syntax:
[rodig]
(brackets): used to match anything found within the brackets. For example: match any one of the letters within the brackets.
[^rodig]
(negative brackets): Used to match anything not found within the brackets. For example, match any character except one of the letters within the brackets.
[D-M]
(range): Used to match a range of characters or numbers. For example, match any characters between the capital letters D and M.
(me|you|us)
(pipe): Used to match alternative options. For example, match either of the words within the parenthesis.
.
(period): Match any characters, expect for a new line character or a line terminator character.
w
(word character): Match any letter, number, or an underscore.
W
(non-word character): Match any character that is not a word characters.
d
(digit): Match any single digit.
D
(non-digit): Match any non-digit character.
s
(space): Match a white space.
S
(non-space): Match any character that's not a space character.
(word boundary): Find a match at the beginning or end of a word.
B
(non-word boundary): Find a match that is not a word boundary.