Book Image

Mastering HTML5 Forms

By : Gaurav Gupta
Book Image

Mastering HTML5 Forms

By: Gaurav Gupta

Overview of this book

HTML5 has given web developers the ability to easily develop sites and applications which, previously, were extremely time consuming. Now, they can not only build visually stunning forms and web pages, but can also increase the scope of their applications, as well as collect valuable user inputs and data through customized forms. This practical guide will teach you how to create responsive forms, and how to link them to the database. This will enable you to take advantage of the power behind HTML5 elements for building forms, and make the user interfaces attractive and more interactive. Explore the benefits of web forms, and learn how to create them using new HTML5 form elements. This guide will take you through a number of clear, practical examples that will help you to take advantage of the forms built and customized using HTML5 and related technologies, quickly and painlessly. Your ability to build responsive forms will be enhanced throughout the book.You will also learn about the necessity of validations, CSS3 properties for improving the look of the form, and how to link the form to the server. Lastly, you will learn to make the standard forms responsive by making them compatible with desktops and mobile devices.
Table of Contents (12 chapters)

The form <input> types, elements, and attributes


The HTML5 forms focus on enhancing the existing simple HTML forms to encompass more types of controls and address the limitations that web developers face today. One of the best things about them is that you can use almost all the new input types, elements, and attributes right now and the HTML5 forms are fully backward compatible. The browser, which supports the new HTML5 elements, enhances their features, otherwise the browser, which does not support it, displays them as a textbox.

In this section, we will learn the new HTML5 <form> elements, such as the <input> types, elements, and attributes that are introduced to enhance the capabilities of forms.

The <form> <input> types

  • date: The date type allows the user to select a date with no time zone.

    It is supported in .

    Syntax:

    <input type= "date" name= "#">

    Attributes:

    • value: The initial value. The format is yyyy-mm-dd

    • min, max: The range in which the smallest and largest dates can be selected

  • datetime: The datetime type allows the user to select a date and a time with the time zone set to UTC.

    The format is yyyy-mm-dd HH:MM.

    It is supported in .

    Syntax:

    <input type= "datetime" name= "#">
  • datetime-local: The datetime-local type allows the user to select a date and time with no time zone. The format used is yyyy-mm-dd HH:MM.

    It is supported in .

    Syntax:

    <input type= "datetime-local" name= "#">
  • color: The color type results in opening a color chooser pop up and is used to choose a color of the <input> type #rrggbb (hex value). It could be represented by a swatch or a wheel picker.

    The value chosen must be a valid simple color's hex value such as #ffffff.

    It is supported in .

    Syntax:

    <input type= "color" id= "#"name= "#">

    Attributes:

    • value: The initial value

  • number: The number type allows the user to input the numbers in either integer or floating point.

    It is also called a spinner.

    We can set restrictions on what numbers are accepted.

    It is supported in .

    Syntax:

    <input type= "number" name= "#">

    Attributes:

    • value: The initial value

    • min, max: The range in which the smallest and largest values can be selected with the up/down arrows

    • step: This tells us how much to change the values when we scroll the spinner

  • range: The range type allows the user to input the numbers in either integer or floating point from a range of numbers. It is displayed in the form of a slider.

    Using this, the exact value is not shown unless you use JavaScript, so use <input type="number" /> if you want the user to choose an exact value.

    We can set restrictions on what numbers are accepted.

    It is supported in .

    Syntax:

    <input type= "range" name= "#">

    Attributes:

    • value: The initial value. The default value is the mid of the slider.

    • min, max: The range in which the smallest and largest values can be selected. The default for min is 0 and max is 100.

    • step: This tells us how much to change the values when we scroll the spinner. The default is 1.

  • email: The email type allows the user to enter the text in e-mail address format [email protected].

    The entered text is automatically validated when clicked on the Submit button.

    If multiple attributes are specified, multiple e-mail addresses can be entered, separated by commas.

    It is supported in .

    Syntax:

    <input type= "email" name= "#">

    Attributes:

    • value: The initial value (a legal e-mail address)

    Using multiple attributes, that is, more than one e-mail ID, is accepted and each attribute is separated by a comma.

  • search: The <input> type search allows the user to enter text that the user wants to search for.

    A search field behaves like a standard text field and has inbuilt clear text functionality, such as the cross button in WebKit browsers.

    It is supported in .

    Syntax:

    <input type= "search" name= "#">

    Attributes:

    • value: The initial value

  • tel: The tel type allows the user to input a telephone number. tel does not provide any default syntax, so if you want to ensure a particular format, you can use pattern to do additional validation.

    No browser support till now.

    Syntax:

    <input type= "tel" name= "#">

    Attributes:

    • value: The initial value as a phone number

  • month: The month type allows the user to select a month and a year with no time zone.

    It is supported in .

    Syntax:

    <input type= "month" name= "#" >

    Attributes:

    • value: The initial value. The format is yyyy-mm.

    • min, max: The range in which the smallest and largest values can be selected.

  • time: The time type allows the user to select a time value with hour, minutes, seconds, and fractional seconds with no time zone.

    It is supported in .

    Syntax:

    <input type= "time" name= "#">
  • url: The url type allows the user to input an absolute URL.

    The entered text is automatically validated when clicked on the Submit button.

    It is supported in .

    Syntax:

    <input type= "url" name= "#" >

    Attributes:

    • value: The initial value as an absolute URL

  • week: The week type allows the user to select a week and a year with no time zone.

    It is supported in .

    Syntax:

    <input type= "week" name= "#">

    Attributes:

    • value: The initial value. The format is yyyy-mmW.

So far we have learned about the various <input> types. Now let's see the new HTML5 <form> elements.

The <form> elements

  • <datalist>: The <datalist> element provides a list of predefined options for form controls to the user as they input data. It is used to provide an autocomplete feature on the <form> elements.

    For instance, if a user enters some text in a text field, a list would drop down with prefilled values that they could choose from.

    It is supported in .

    For example:

    <input list= "browsers" name= "browser">
    <datalist id= "browsers">
      <option value= "Internet Explorer">
      <option value= "Firefox">
    </datalist>
  • <keygen>: The <keygen> element is used to provide a secure way to authenticate users.

    When the form is submitted, the private key is stored in the local keystore and the public key is packaged and sent to the server.

    It is supported in .

    For example:

    <form action= "keygen.html" method= "get"><input type= "text" name= "username"><keygen name= "security"><input type= "submit">
    </form>
  • <output>: The <output> element represents the result of a calculation performed like the one performed by scripts.

    It is supported in .

    For example:

    <form onsubmit="return false" oninput="o.value=parseInt(a.value)+parseInt(b.value)"><input name="a" type="number" step="any">+<input name="b" type="number" step= "any">
    =<output name="o"></output></form>

Now let us see the new HTML5 <form> attributes.

The <form> attributes

  • autocomplete: The autocomplete attribute allows users to complete the forms based on earlier inputs. We can have an autocomplete on option for the form and an off option for specific input fields or vice versa.

    It works with both the <form> and <input> types such as textbox, datepicker, range, color, url, tel, search, and email.

    It is supported in .

    For example:

    <input type="text" name="city" autocomplete="on">
  • autofocus: When the autofocus attribute is added, an <input>type automatically gets focus when the page loads.

    For instance, when we open the Google home page or any search engine, the focus automatically goes to the textbox where a user enters the text to perform a search.

    It works with the <input> types, textbox, search, url, email, tel, and password.

    It is supported in .

    For example:

    <input type="text" name="city">
    <input type="text" name="state" autofocus>
  • placeholder: The placeholder attribute gives users a hint that describes the expected value of an <input> field.

    It disappears when the control is clicked on or gains focus.

    It should be used only for short descriptions or else use the title attribute.

    It works with the <input> types, textbox, search, url, email, tel, and password.

    It is supported in .

    For example:

    <input type="text" name="name" placeholder="First Name">
  • min and max: The min and max attributes are used to specify the minimum and maximum value to an <input> type.

    It works with the <input> types, number, range, date, datetime, datetime-local, month, time, and week.

    It is supported in .

    For example:

    <input type="number" min="1" max="5">
  • list: The list attribute refers to a <datalist> element that contains predefined options for an <input> element.

    It is used to provide an autocomplete feature on the <form>elements.

    For instance, if a user enters some text in a text field, a list would drop down with prefilled values from which they could choose.

    It works with the <input> types, textbox, search, url, email, tel.

    It is supported in .

    For example:

    <input list= "browsers" name= "browser">
    <datalist id= "browsers">
      <option value= "Internet Explorer">
      <option value= "Firefox">
    </datalist>
  • formnovalidate: The formnovalidate attribute specifies that the form should not be validated during submission. It overrides the novalidate attribute of the <form>elements.

    It works with the <input> types, submit and image.

    It is supported in .

    For example:

    <input type="email" name="email"><input type="submit" formnovalidate value="Submit">
  • form: The form attribute specifies one or more forms that an <input> type belongs to, or in other words, it allows the users to associate any orphaned form control with any <form> element on the page.

    It is supported in .

    For example:

    <body>
      <form action="form.html" id="form1"><input type="text" name="fname"><br><input type="submit" value="Submit"></form>
      <p>The "Last name" field below is outside the form element, but it is still a part of the form</p>
      <input type="text" name="lname" form="form1">
    </body>
  • formaction: The formaction attribute specifies the URL of a file or application that will submit the form.

    It works with the <input> types, submit and image.

    It is supported in .

    For example:

    <input type="submit" value="Submit" formaction="form.html">
  • formenctype: The formenctype attribute specifies how the form data is encoded when submitting to the server.

    It works with the post method only.

    It works with the <input> types, submit and image.

    It is supported in .

    For example:

    <input type="submit" value="Submit" formenctype="multipart/form-data">
  • formmethod: The formmethod attribute specifies which HTTP method such as GET, POST, PUT, and DELETE will be used to submit the form data.

    It works with the <input> types, submit and image.

    It is supported in .

    For example:

    <input type="submit" value="Submit" formmethod="post">
  • formtarget: The formtarget attribute specifies the target window to display the response received after submitting the form.

    It works with the <input> types, submit and image.

    It is supported in .

    For example:

    <input type="submit" value="Submit" formtarget="_self">

    Values:

    • blank

    • self

    • parent

    • top

    • framename

  • multiple: The multiple attribute allows users to enter more than one value to the <input> types.

    It works with the <input> types, email and file.

    It is supported in .

    For example:

    <input type= "file" name= "image"multiple>
  • novalidate: The novalidate attribute specifies that the form should not be validated when the Submit button is clicked.

    It is supported in .

    For example:

    <form action= "form.html" novalidate>
      <input type= "text" name= "city">
      <input type= "text" name= "state" autofocus>
    </form>
  • step: Let us understand the step attribute with an example. If step= 2, legal numbers could be 2, 0, 2, 4, and 6.

    It works with the <input> types, number, range, date, datetime, datetime-local, month, time, and week.

    It is supported in .

    For example:

    <input type= "range" name= "#" step= "2">
  • required: The required attribute, when added, enforces that an input field must be filled out before submitting the form.

    Currently, the error messages are specific to the browsers and cannot be controlled by the CSS.

    It replaces the basic <form> validations that were implemented with JavaScript, thus saving development time.

    It is supported in .

    For example:

    <input type= "text" name= "city"required>
  • pattern: Using the pattern attribute, you can declare your own requirements for validation using Regular Expressions (regex).

    It works with the <input> types, text, search, url, tel, email, and password.

    In case, the value entered by a user does not match the pattern, it will display a browser generic message.

    It is supported in .

    For example:

    <input type= "text" name= "country_code" pattern= "[A-Za-z]{3}" placeholder= "Three letter country code">