HTML Form Tag

Forms can be used to send data across the web and are often used as contact form to convert information input by a user into Leads.
HTML forms are used to pass data to the server.
The common elements used in the actual HTML form are form tag, input, textarea,, select, and label.
Form Tag defines the form and within this tag, there is action attribute which tells the form where its contents will be sent to when it is submitted.
An HTML form can contain input elements like text fields, checkbox, radio buttons, submit button and more. A form can also contain select list, textarea, fieldset, legend, and label elements.

Create HTML Form

form is build inside <form> tag. See the code below


<form action="" >
	/*Content*/
</form>

Form inside fieldset with legend

Form can also be placed inside fieldset tag. Fieldset tag is used to group form. We can also use legend tag inside fieldset.

Enquiry Form


<fieldset>		
<legend> Enquiry Form</legend>		
<form action="" >
	<label>Name:<input type="text"></label>
    <input type="reset">
    <input type="submit">
</form>
</fieldset>

Fieldset with disable

Fieldset also supports disable attribute. By adding disable attribute, all form controls including submit button are disabled. Thus user cannot fill and submit form data.

Enquiry Form
Name:


<fieldset disable>		
<legend> Enquiry Form</legend>		
<form action="" >
	<label>Name:<input type="text"></label>
    <input type="reset">
    <input type="submit">
</form>
</fieldset>

Input Element

The most important form element is the input element.
The input element is used to get user information.
An input element can vary in many ways, depending on the type attribute.
An input element can be of type text field, checkbox,password, radio button, submit button, and more.


Attributes in input element

Attribute Name values Use
type text, password, file, radio, checkbox, button, submit, and reset type defines type of input control.
size default value is 20 change size of input control
tabindex any numeric value used to define a sequence followed by user when he navigate using Tab key
value any possible value set a default value of input control
maxlength n digits set maximum characters length
disabled "" disabled input control, or fieldset tag
checked "" choose checkbox and radio button

Input Type Text

Input type text <input type="text"> is the common input element for name, surname, country, numbers and symbols. Default input type is text. Even if we skip type attribute in input element, default input type is text. But still it is recommended to define type atribute in input element.

HTML Input type text


First Name: <input type="text">  
Last Name: <input type="text"> 

HTML Input with label

Label is used to write the content just before text field. To Specify particular label, the value of for attribute inside label should match the id of input control..



<label>First Name:<input type="text"></label>

<label for="lname">Last Name:</label>
<input type="text" id="lname"> 


Input Type Text with value




<form>
<label for="fname">First Name:</label> <input type="text" value="First Name" id="fname" >
<label for="lname">Last Name:</label><input type="text" value="Last Name" id="lname" >
</form>

Input Type Text with maxlength


maxlength attribute is used to restrict no of characters in a text field.

Input with maxlength



<form>
<label for="fname">First Name:</label><input type="text" id="fname" maxlength="10">
<label for="lname">Last Name:</label><input type="text" id="lname" maxlength="10">
</form>

Input type Password

The input type password is used to write passwords. The password value is written in encrypt form. i.e.
we cannot see, copy or cut password data from input type password.


<form>
<label for="pwd">Password:</label> 
<input type="password" id="pwd" >
</form>

Input type File

Input type file let user to choose file from his system. This can be used to uplaod a picture, upload resume, upload a video or audio etc.

Default value of input type file is "No file chosen". Once the file is uploaded, the file name replace this text.


<form>
<label for="resume">Resume:</label> 
<input type="file" id="resume" >
</form>


Radio Buttons

Radio Buttons are used to choose a single element among a group.
To allow window to choose a single radio, use name attribute with same value on both radio inputs.

Create Radio Button group




    <input type="radio" name="gender" id="male"> 
    <label for="m">Male</label>
    
    <input type="radio" name="gender" id="female"> 
    <label for="f">Female</label>

Checkbox

Checkbox are used to select multiple selections unlike radio button. They can be checked and unchecked. We can use checkboxs for hobbies, interestes, terms & conditions, etc.

HTML Checkbox




<label> <input type="checkbox"> :Bike</label>
<label> <input type="checkbox"> :Car</label>

Checkbox with disabled

Checbox can also have disabled attribute. A disabled checbox can't be checked, means checked will remain checked, and unchecked will remain unchecked. See example

HTML Checkbox with disable


<label> <input type="checkbox" disabled> :I Agree</label>

Checkbox with checked

Default checbox state is unchecked. But we can change default state to checked by using checked attribute in input type checkbox. See example

HTML Checkbox with checked



<label> <input type="checkbox" checked> :I Agree</label>
<label> <input type="checkbox" disabled checked> :I Agree</label>


Select Dropdown list

select or select dropdown is used to fetch single or multiple field among listed options

Select dropdown


	<form>
		<label for="city">Current City:</label>
		<select id="city">
			<option>--Select City--</option>
			<option>New Delhi</option>
			<option>Kolkata</option>
			<option>Mumbai</option>
			<option>Chennai</option>
		</select>
	</form>

(How Select Looks like on browser)


Select with Optgroup

Select with multiple Optgroup


<form>
	<label for="city">Current City:</label>
	<select id="city">
        <option>--Select City--</option>
		<optgroup label="Metros">
			<option>New Delhi</option>
			<option>Kolkata</option>
			<option>Mumbai</option>
			<option>Chennai</option>
			</optgroup>
		<optgroup label="Others">
			<option>Noida</option>
			<option>Gurgram</option>
			<option>Faridabad</option>
			<option>Gaziabad</option>					
		</optgroup>
	</select>
</form>

Select with multiple options

Select with multiple Optgroup


<form>
	<label for="city">Current City:</label>
    
	<select id="city" multiple>
            <option>--Select City--</option>
		<optgroup label="Metros">
			<option>New Delhi</option>
			<option>Kolkata</option>
			<option>Mumbai</option>
			<option>Chennai</option>
			</optgroup>
		<optgroup label="Others">
			<option>Noida</option>
			<option>Gurgram</option>
			<option>Faridabad</option>
			<option>Gaziabad</option>					
		</optgroup>
	</select>
</form>
Press Ctrl for multi selection.

Choose Date Of Birth using SELECT



<select>
    <option>2000</option>
    <option>2001</option>
    <option>2002</option>
    <option>2003</option>
    <option>2004</option>
    <option>2005</option>
    <option>2006</option>
    <option>2007</option>
    <option>2008</option>
    <option>2009</option>
    <option>2010</option>
    <option>2011</option>
    <option>2012</option>
    <option>2013</option>
    <option>2014</option>
    <option>2015</option>
    <option>2016</option>
    <option>2017</option>
</select>

<select>
    <option>JAN</option>
    <option>FEB</option>
    <option>MAR</option>
    <option>APR</option>
    <option>MAY</option>
    <option>JUN</option>
    <option>JUL</option>
    <option>AUG</option>
    <option>SEP</option>
    <option>OCT</option>
    <option>NOV</option>
    <option>DEC</option>
</select>

<select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
    <option>16</option>
    <option>17</option>
    <option>18</option>
    <option>19</option>
    <option>20</option>
    <option>21</option>
    <option>22</option>
    <option>23</option>
    <option>24</option>
    <option>25</option>
    <option>26</option>
    <option>27</option>
    <option>28</option>
    <option>29</option>
    <option>30</option>
    <option>31</option>
</select>

Date Of Birth


Select:



Textarea

Textarea is used to write multiple line. Like query, address, comments, reviews etc.

(How to create a Query field)


		
<form>
<textarea name="textarea" cols="50" rows="5"></textarea>
</form>

(How a Query field Looks like on browser)


Submit Button

Submit Button or input type submit is used to send information from user to web server. Submit button can be used only once in a form tag.

Submit Button example


<form>
<input  type="submit"  value="submit">
</form>

(How a Submit field Looks like on browser)


Reset Button

Reset Button or input type reset is used to reload form data, without refreshing webpage. Reset is also used once in a form tag.

Create reset Button


<form>
<input type="text">
<input type="reset">
</form>

(How a Submit field Looks like on browser)


Button Tag

Button Tag or Button can alose create buttons, like input type button. Button Tag is pair element. We can use image, icon or child element inside button tag.

Type of buttons

  • Button,   <button>Button</button>.
  • Reset button,   <button type="reset">Button</button>.
  • Submit button,   <button type="submit">Button</button>.

Button Tag Example



<button>Button 1</button>
<button type="button">Button 2</button>
<button type="reset">Reset</button>
<button type="submit">Submit</button>

Inside form element, button will work as submit button, but outside form, button will work as button.


Html5 Form

These form controls are HTML4 Based. To learn HTML5 based Form controls and attributes, click here HTML5 Form