Introduction to HTML5 Forms HTML5 Web forms are those sections on the Web page that contain special elements called as controls. The controls, such as check boxes, radio buttons, and text boxes provide a visual interface to the user to interact with them. A user provides data through these controls that is sent to the server for further processing. In HTML5, creation of form is made easier for Web developers by standardizing them with rich form controls. It also provides client-side validations that are now handled natively by the browsers. This reduces the load time of the pages and also removes the need of the repetitive JavaScript codes to be included
HTML5 Web forms bring great improvements related to form creation for the Web developers and also for users interacting with them. The following are the changes introduced in HTML5 forms: New form elements New input types New attributes Browser-based validation CSS3 styling techniques Forms API
New 1-2is a data field that allows the TheInput inputTypes element user to edit the data on the form. It has an attribute named type which controls the
data type and characteristics of the input element. Following table lists the new input types supported by HTML5. Type
Description
email
Represents the completion progress of a task on the page
search
Represents a scale of known range
url
Represents a set of options used with list attribute to make a drop-down control
New Attributes HTML5 has introduced several new attributes that
can be used with form and input elements. Attributes help the elements to perform their tasks. Type Following table lists the new attributes in HTML5. Description
A Web developer can enhance the form elements CSS Styling with the Techniques pseudo-class 1-2 selectors, such as :required, :valid, and :invalid. The input fields which cannot be left blank while submitting the form can be displayed with an outline by styling the input field using CSS.
HTML5 has introduced JavaScript API for forms to Forms API
customize validations and processing performed on the forms. The new Forms API provides new methods, events, and properties to perform complex validations combining Events and fields or calculations. Description Methods Following table lists the events and methods.
setCustomVa lidity (message)
Sets the custom error message that is displayed when the form is submitted by the user
checkValidi ty()
Checks the validity of the e-mail address entered by the user
oninvalid
Allows script to run only when the element is invalid
The type Working withattribute New Input Types of the input element determines what kind of input will be displayed on the user’s browser. The default input is type=”text”. text The registration form in the session is using the following
The type=”email” E-mail Address 1-2 is used for specifying one or
more e-mail addresses. To allow multiple addresses in the e-mail field, separate each address with comma-separator. In the registration form, the input type is changed from text to email as shown in the following method=”get” action=”test.html”> In the code snippet, <label> tag defines a label
code snippet: for=”emailid”>Email:</label> for the element on the form. The for attribute of <label> tag binds it with type=”email” value=”” id=”emailid” the related element, that is email element, on the form. name=”emailaddress” The value of for/>attribute must match with the maxlength=”255” value of id attribute assigned to the element. type=”submit” value=”submit”/> The email contains two attributes, namely id </form> and name. specifies
The name attribute specifies a name for the E-mail Address input element.2-2 The look of the input is still like a plain text field, but changes are applied behind the scenes. Browsers, such as Firefox, Chrome, and Opera will display a default error message if user submits the form with some unrecognizable contents. Following figure shows the error message for incorrect E-mail Address in Chrome.
specifying a Web address. The look of the url field is a normal text field. The Code Snippet shows the code of url input
type. for=”url”>Enter your Web page Browsers, such as Opera, Firefox, and Chrome address:</label> supporttype=”url” validation forvalue=”” the url input type. While validating the URL, browsers only checks for id=”urlname” name=”urltext” entry with forward slash (/)./> maxlength=”255” Following figure shows the error message for <input type=”submit” value=”submit”/> incorrect URL in Chrome.
accepting telephone numbers. Telephone The tel Number type does not impose a particular pattern. It supports characters, numbers, and special
characters except new lines or carriage returns. A user can enforce a pattern for tel input type by using placeholder, pattern attribute, or a JavaScript code for performing client-side validation. for=”telno”>Telephone The Code Snippet shows the code for including Number:</label> input type on the registration type=”tel” value=””form. id=”telno” name=”telephone_no” maxlength=”10” />
The input type=”number” is used for accepting only number values. Number The input element displayed for number type is a spinner box. The user can either type a number or click the up or down arrow to select a number in the spinner
box. for=”stud_age”>Age:</label> The Code Snippet shows the code for including type=”number” value=”15” inputsnippet, type on the the form. number In the code number input type has id=”stud_age”
attributes, such as min and max to specify the name=”studentage” min=”15” minimum and maximum value for the input. max=”45” step=”1” /> Following figure shows the input type in Opera. <input type=”submit” value=”submit”/>
The input type=”range” is similar to number Range type 1-2 and displays a slider control on the page. The range type is used when the exact value is not required in the input. For example, an online survey form asking the clients to rate the products may not receive exact the ratings. values In codein snippet, the range input type contains The Code such Snippet shows thestep, code and for value. including attributes, as min, max, <label>Survey for packages offered[scale: input type withattributes
attributesare min andto max. 1-10]:</label> range The min and max used specify the minimum and maximum value allowed for a type=”range” name=”rating” min=”1” max=”10” range and/>are set to 1 and 10 respectively.
The step attribute specify the intervals for type=”submit” value=”submit”/>
HTML5 introduced Date and has Time 1-7 several new input types for
date and time. The format for all these date and time types is according to the ISO standards. At present only Opera provides the support for Date This input type contains only date in year, month, date element by displaying calendar and day format. The time apart is notcontrol. support by date type. The Code Snippet shows the code of the date input type. for=”stdate”>Date:</label>
id=”stdate” In the type=”date” code snippet, all date input types have min name=”startdate”
and max attributes to set the range for the dates. min=”2000-01-01”/> The
type=”month” includes only the year and month in the input. The Code Snippet shows the syntax of month input Browser such as Opera will display the date picker
type. for=”stmonth”>Month:</label> for selecting month. type=”month” id=”stmonth” On selecting any day/> from the calendar, the whole name=”startmonth” monthtype=”submit” is selected. value=”submit”/> Following figure shows the date picker for the month input type.
The input type=”time” displays a time of day in hours and minutes format (24-hour clock). The Code Snippet shows the syntax of the time input type. <label>Time:</label> type=”time” name=”time” /> Following
figure shows the time input type in Opera.type=”submit” value=”submit”/>