With web forms, you can collect information from users. Web forms can collect anything like name, email address, message, images, files from your computer, and so on.
When you fill out a form, the information is sent to the web server. In HTML, forms are created with the <form> element. The action attribute tells the form where to go or what to do when the user clicks Submit. For example,
<input type="text" name="emailaddress">
<input type="submit" name="submit">
There are many ways to get input through a form, each with its own specific name or type of input.
Most of the form elements are variations of the same tag. The <input> tag can be used to create single-line text boxes, password boxes, buttons, and even invisible content (such as hidden fields). The input element has three common attributes:
HTML5 added several new input types. New input types that are not supported by older web browsers behave as text input. HTML5 has a total of 13 new input types:
A date and time (year, month, day, hour, minute, second, fractions of a second) encoded according to ISO 8601 with the time zone set to UTC. This includes both the date and time, separated by a "T".
A date and time (year, month, day, hour, minute, second, fractions of a second) encoded according to ISO 8601, with no time zone information.
A date (year, month, day) encoded according to ISO 8601. This comprises the date (year, month, and day), but no time.
A date consisting of a year and a month encoded according to ISO 8601.
A date consisting of a year and a week number (from 1 to 52) encoded according to ISO 8601.
A time (hour, minute, seconds, fractional seconds) encoded according to ISO 8601.
The number type provides an input for entering a number. The step attribute specifies the precision, defaulting to 1. Usually, this is a spinner box, where you can either enter a number or click on the up or down arrows to select a number. The number input has min and max attributes to specify the minimum and maximum values allowed. You can also provide a step attribute, which determines the increment by which the number steps up or down when clicking the up and down arrows.
The range input type displays a slider control. It is used for input fields that should contain a value from a range of numbers. As with the number type, it allows the min, max, and step attributes. The difference between number and range is that the exact value of the number is unimportant with range. It is ideal for inputs where you want an imprecise number. For example, a customer satisfaction survey asking clients to rate aspects of the service they received.
The default value of a range is the midpoint of the slider - halfway between the minimum and the maximum.
The email type is used for specifying one or more email addresses. This type is used for input fields that should contain an email address. If you try to submit a form with content unrecognizable as one or more email addresses, the browser will tell you what is wrong.
The url input is used for specifying a web address. This type is used for input fields that should contain a URL address.
The search input type provides a search field, a one-line text input control for entering one or more search terms. The difference between the text state and the search state is that on platforms where search fields are distinguished from regular text fields, the search state might result in an appearance consistent with the platform's search fields rather than appearing like a regular text field.
For telephone numbers, use the tel input type. All over the world countries have different types of valid phone numbers, with various lengths and punctuation, so it would be impossible to specify a single format as standard. You can encourage a particular format by including a placeholder with the correct syntax. Additionally, you can stipulate a format by using the pattern attribute.
The color input type provides the user with a color picker. The color picker should return a hexadecimal RGB color value, such as #FF3300.
he <select> element defines a drop-down list. The <option> elements defines an option that can be selected. By default, the first item in the drop-down list is selected. To define a pre-selected option, add the selected attribute to the option.
In HTML5, there is the wrap attribute. This attribute applies to the textarea element, and can have the values soft (the default) or hard. With soft, the text is submitted without line breaks other than those actually entered by the user, whereas hard will submit any line breaks introduced by the browser due to the size of the field. If you set the wrap to hard, you need to specify a cols attribute.
A fieldset is a special element used to supply a visual grouping to a set of form elements. The <fieldset> element is used primarily for layout and accessibility. The <legend> element, a part of the fieldset, creates the heading of the fieldset and a box surrounds the inputs in the form. Like <fieldset>, the <legend> element is entirely optional.
The <div> elements used to create each row of inputs. The <label> elements are used to show friendly name of what an input does.
<input id=”username” type=”text” name=”username”>
<label for=”email”>E-mail Address:</label>
<input id=”email” type=”text” name=”email”>
The action of a form points to the server program that will handle the input from the form. It is where the form sends its data. In HTML5, the forms no longer need to have the action attribute defined. If omitted, the form will behave as though the action were set to the current page.
The method attribute tells the form how to send the data to the server. There are two primary methods for this - GET and POST. The GET method is appropriate for small forms, whereas the POST method is appropriate for larger forms or ones that need to send a lot of information.
When you use a GET method, the form’s contents are sent as part of the URL.
1. The required Attribute
HTML5 introduced a new attribute called required which insists to have a value. The Boolean required attribute tells the browser to only submit the form if the field in question is filled out correctly. Obviously, this means that the field can’t be left empty, but it also means that, depending on other attributes or the field’s type, only certain types of values will be accepted.
<input type="text" name="search" required />
If a required field is empty or invalid, the form will fail to submit, and focus will move to the first invalid form element. The required attribute can be set on any input type except button, range, color, and hidden, all of which generally have a default value.
2. The placeholder Attribute
HTML5 introduced a new attribute called placeholder. This attribute provides a hint to the user of what can be entered in the field. The placeholder text disappears when the field gains focus, and reappears on blur if no data was entered.
<input type="text" name="search" placeholder="Search the web" />
3. The pattern Attribute
The pattern attribute enables you to provide a regular expression that the user’s input must match in order to be considered valid. For any input where the user can enter free-form text, you can limit what syntax is acceptable with the pattern attribute.
When including a pattern, you should always indicate to users what is the expected (and required) pattern.
4. The disabled Attribute
The Boolean disabled attribute can be used with any form control except the output element. HTML5 allows you to set the disabled attribute on a fieldset and have it apply to all the form elements contained in that fieldset.
The form elements with the disabled attribute have the content grayed out in the browser. The text is lighter than the color of values in enabled form controls. Form controls with the disabled attribute are not submitted along with the form. So, their values will be inaccessible to your form processing code on the server side. If you want a value that users are unable to edit, but can still see and submit, use the readonly attribute.
5. The readonly Attribute
The readonly attribute is similar to the disabled attribute. It makes the form element impossible for the user to edit the form field. However, unlike disabled, the field can receive focus, and its value is submitted with the form.
6. The multiple Attribute
The multiple attribute indicates that multiple values can be entered in a form control. While it was available in previous versions of HTML, it only applied to the select element. In HTML5, it can be added to email and file input types as well. If present, the user can select more than one file, or include several comma-separated email addresses.
7. The form Attribute
The form attribute allows you to associate form elements with forms in which they are not nested. This means you can now associate a fieldset or form control with any other form in the document. The form attribute takes as its value the id of the form element with which the fieldset or control should be associated.
If the attribute is omitted, the control will only be submitted with the form in which it is nested.
8. The autocomplete Attribute
The autocomplete attribute specifies whether the form, or a form control, should have autocomplete functionality. For most form fields, this will be a drop-down that appears when the user begins typing. For password fields, it is the ability to save the password in the browser.
By default, autocomplete is on. In order to disable it, use autocomplete="off". This is a good idea for sensitive information, such as a credit card number, or information that will never need to be reused, like a captcha.
9. The datalist Element and the list Attribute
It is a text field with a set of predefined autocomplete options. Unlike the select element, the user can enter whatever data they like, but they will be presented with a set of suggested options in a drop-down as they type.
The datalist element, much like select, is a list of options, with each one placed in an option element. You then associate the datalist with an input using the list attribute on the input. The list attribute takes as its value the id attribute of the datalist you want to associate with the input. One datalist can be associated with several input fields.
10. The autofocus Attribute
The Boolean autofocus attribute specifies that a form control should be focused as soon as the page loads. Only one form element can have autofocus in a given page.