HTML Tags – Input Tag


What is HTML <input> tag ?

HTML <input> tag represents a form input control in HTML document.

HTML <input> tag is used to create an interactive input control inside the HTML document.

HTML <input> tag accepts the input data from user inside the HTML document.

HTML <input> element is the most important and commonly used form element.

How HTML <input> tag works ?

HTML <input> tag specifies an input field where the user can enter data.

HTML <input> tag is most commonly used inside the <form> tag.

And when the user submit’s the form, all input data is passed from client to server.

Why HTML <input> tag is used ?

HTML <input> control facilitate’s the user to input data and communicate with a website or application.

Since there are wide number of combinations for input types and attributes, its commonly used inside the web applications.

HTML beginners tutorial !!!

Click to Learn More about – HTML Tutorial for beginners

Syntax :

<input type = "value" .... />

HTML5 introduces the new input types to improve the user experience and to make the HTML forms more interactive.

HTML5 new input types are listed below,


SnoTypeDescription
1colorRepresents an input field which defines a color selector.
2dateRepresents an input field to define a date selector.
3datetimeDefines full date and time display with time zone information.
4datetime-localDefines date and time without time zone information.
5emailDefines an input field with email pattern Validation.
6monthDefines the input field to enter month for the particular year
7numberDefines field which selects a numeric value only.
8rangeDefines a numeric value selector with a given range of 1 to 100.
9searchThis is used to define a search field.
10telRepresents a control to enter a telephone number.
11timeRepresents a control to enter time value with no time zone.
12urlRepresents an input field to enter a URL
13weekDefines a selector for week value for the particular year.

1. Color

Color input type allows the user to select a color from a color picker and returns the color value.

The color value will be returned in hexadecimal format (#rrggbb).

If we don’t we specify the value, the default value #000000 (black color) is returned.

Color is supported by web-browsers like Firefox, Chrome, Opera, Safari (12.1+), Edge (14+).

Color is not supported by web-browsers like Microsoft Internet Explorer and older version of Apple Safari browsers.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
           Purple : <input type="color" value="#651a77" id="mycol1"><br/>
           Red : <input type="color" value="#F12416" id="mycol2"><br/>
           Blue : <input type="color" value="#2A16F1" id="mycol3"><br/>
           Brown : <input type="color" value="#926F12" id="mycol4"><br/>
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example1-cc.png

2. Date

Date input type allows the user to select a date from a drop-down calendar.

By default, Date value includes the year, month, and day, but not the time.

Date is supported by the Chrome, Firefox, Opera and Edge browsers.

Date is Not supported by the Internet Explorer and Safari browsers.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
           Date :  <input type="date" value="2019-04-15" id="mydate"><br/>
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example2-cc.png

3. Datetime

Datetime is used to specify the date and time of the element.

By default this displays in the format year, month, date, hour, minute and seconds (YYYY-MM-DDThh:mm:ssTZD).

Datetime is supported by Chrome, Edge, and Opera browsers.

Datetime is not supported by Firefox, Safari, and Internet Explorer browsers.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
            DateTime
            <p>The concert took place on
            <time>Jan 18,1985 at 7:30 pm</time>.</p>
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example3-cc.png

4. Datetime-local

Datetime-local input type allows the user to select local date and time.

This includes year, month, and day as well as the time in hours and minutes.

Datetime-local is supported by Chrome, Edge, and Opera browsers.

Datetime-local is not supported by Firefox, Safari, and Internet Explorer browsers.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
           Date Time Local :     <input type="datetime-local" id="mydatetime">
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example4-cc.png

5. Email

Email input type allows the user to enter e-mail address.

The user can enter the properly-formatted e-mail address in the input.

Email is supported by all major browsers like Firefox, Chrome, Safari, Opera, Internet Explorer 10 and above.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
           Email : <input type="email" id="myemail" required><br/>
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example5-cc-1024x477.png

6. Month

Month input type allows the user to select a month and year from a drop-down calendar.

String returned will be in the format “YYYY-MM”, where YYYY is the four-digit year and MM is the month number.

Month is supported in Chrome, Edge, and Opera browsers.

Month is not supported by Firefox, Safari and Internet Explorer browsers.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
        Month : <input type="month" id="mymonth">
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example6-cc.png

7. Number

Number input type is used for entering a numerical value.

HTML5 allows us to specify the range using attributes like min, max.

Number is supported by all major web browsers such as Firefox, Chrome, Safari, Opera, Internet Explorer 10 and above.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
            Number :     <input type="number" min="1" max="10" step="0.5" id="mynumber">
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example7-cc.png

8. Range

Range input type is used for entering a numerical value within a specified range.

This is similar to number input, but it offers a simpler control for entering a number.

Range is supported by all major web browsers such as Firefox, Chrome, Safari, Opera, Internet Explorer 10 and above.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
        Range :     <input type="range" min="1" max="10" step="0.5" id="mynumber">
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example8-cc.png

9. Search


The search input type is used for creating search input fields.

This looks like normal text field.

Search  is supported by all major web browsers such as Firefox, Chrome, Safari, Opera, Internet Explorer 10 and above.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
            Search :     <input type="search" id="mysearch">
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example9-cc.png

10. Tel

The tel input type is used for entering a telephone number.

By default Web-browsers don’t support tel input validation.

Either we can instruct the user with placeholder attribute or use regular expression to validate the input phone numbers.

Tel is not supported by any browser because format for phone numbers vary so much across countries.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
            Tel :     <input type="tel" id="myphone" placeholder="xx-xxxx-xxxx" required>
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example10-cc-1024x461.png

11. Time

Time input type is used for entering a time (hours and minutes).

Web Browsers may use 12- or 24-hour format for input times.

Time is not supported by Internet Explorer and Safari browsers. Currently supported by Chrome, Firefox, Edge, and Opera browsers.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
            Time :     <input type="time" id="mytime">
    </form>
</body>
</html>

Output :

This image has an empty alt attribute; its file name is html5-3-example11-cc.png

12. URL

URL input type is used for entering URL’s or web addresses.

Multiple URL’s can be entered using multiple attribute.

URL is supported by all major browsers like Firefox, Chrome, Safari, Opera, Internet Explorer 10 and above.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Input Type</title>
</head>
<body>
    <form>
            URL :      <input type="url" id="myurl" required>
    </form>
</body>
</html>

Output :


This image has an empty alt attribute; its file name is html5-3-example12-cc.png

13. Week

Week input type allows the user to select a week and year from a drop-down calendar.

Week is not supported by Firefox, Safari and Internet Explorer browsers. Currently supported by Chrome, Edge, and Opera browsers.

Example :

Output :

This image has an empty alt attribute; its file name is html5-3-example13-cc-1024x591.png

<input> Tag Attributes

HTML <input> tag support following specific attributes.


SnoAttributesValueDescription
1acceptfile_extension
MIME_type
audio/*
image/*
video/*
Specify file type that accept the server. Value must be comma separated. Following are valid values:
File extension Specifies the file extension. e.g. .png, .jpeg, .pdf etc…
MIME type Specifies the valid MIME Type of file.
audio/* Representing sound files.
image/* Representing image files.
video/* Representing video files.
2alttextSpecify the alternate text only when input type value image.
3autocompleteon
off
Specify autocomplate should be enable or not.
4autofocusautofocusSpecify that a input should be focus automatically when the page loads.
5dirnameinputname.dirSpecifies text direction of <input> element when will submitted the form.
6formform_idSpecify one or more forms that associate with input element.
7formactionURLSpecify the URL that will process the input element when the form is submitted.
8formenctypeapplication/x-www-form-urlencoded
multipart/form-data
text/plain
Specify the encoding method to be used for data when the form’s data is submitted.
9formmethodget
post
Specify the method to use when sending form’s data.
10formnovalidateformnovalidateSpecify that input element should not be validated when submitted.
11formtarget_blank
_self
_parent
_top
Specify where to display the results after submitting form data only when form type submit or image.
12heightpixelsSpecify that input element should not be validated when submitted.
13listdatalist_idSpecify the <datalist> tag that contains pre-defined options for an input element.
14maxnumber
date
Specify the maximum (numeric or date-time) value for an input element.
15maxlengthnumberSpecify the maximum number of characters allowed in an input element (If input type attribute value is text, email, search, password, tel, or url).
16minnumber
date
Specify the minimum (numeric or date-time) value for an input element.
17minlengthnumberSpecify the minimum number of characters allowed in an input element (If input type attribute value is text, email, search, password, tel, or url).
18multiplemultipleSpecify that allow to enter one or more values in an input element.
19nametextSpecify input element name.
20patternregular_expressionSpecify regular expression pattern that must be match to value.
21placeholdertextSpecify short hint that describe to the user of what value should be enter in the input element.
22readonlyreadonlySpecify that input element should be read-only, user can not modify it.
23sizenumberSpecify the width of input character input must be required to filled value.
24srcURLSpecify the URL of the image that use in submit button of input element (only when type=”image”).
25stepnumberSpecify that number interval of an input element. This attribute work with min and max attributes.
26typebutton
checkboxcolor
date
datetime
datetime-local
email
file
hidden
image
month
number
password
radio
range
reset
search
submit
tel
text
time
url
week
Specify the input type.
27valuetextSpecify the value of an input element.
28widthpixelsSpecify the width of an input element (only when type=”image”).
Following attributes has been removed in HTML5.
29alignleft
right
top
middle
bottom
Specify the alignment of an input element (only when type=”image”).

Global Attributes

HTML <input> tag support following global attributes.

SnoAttributesValueDescription
1idunique_nameDeclared unique id for an element.
2classclass_nameDeclared one or more classnames for an element.
3stylestylesCSS inline styles specify an element.
4titletitleSpecify extra details of element contain, this will display as a “tooltip” for an elements.

Event Attributes

HTML <input> tag support following event attributes.

SnoAttributesValueDescription
1onfocusscriptelement gets focus on object when script tobe run.
2onblurscriptelement lose the focus on object when scrip tobe run.
3onabortscriptelement gets aborted on object when script tobe run.
4onchangescriptelement gets anytime change on object when script tobe run.
5onbeforeunloadscriptelement gets unloaded on object when scrip tobe run.
6onclickscriptclicked on object when script tobe run.
7ondblclickscriptdouble click on object when script tobe run.
8onkeydownscriptkey is pressed when script tobe run.
9onkeypressscriptkey is pressed over element then released when script tobe run.
10onkeyupscriptkey is released over element when script tobe run.
11onmousedownscriptmouse button was pressed over an element when script tobe run.
12onmouseoutscriptmouse pointer release over an element when script tobe run.
13onmousemovescriptrun mouse pointer moved when script tobe run.
14onmouseoverscriptrun mouse pointer move over when script tobe run.
15onmouseupscriptmouse button is released when script tobe run.
16onresetscriptform has been reset when script tobe run.
17onselectscriptSelect some content when script tobe run.
18onsubmitscriptform has been submitted when script tobe run.
19onloadscriptobject has load when script tobe run.
20onchangescriptallow to change the object when script tobe run.
21onunloadscriptunload to the browser window when script tobe run.
22ondragscriptelement being dragged when script tobe run.
23ondragendscriptelement being stop dragged when script tobe run.
24ondragenterscriptelement being go target dragged when script tobe run.
25ondragleavescriptelement being leave to target dragged when script tobe run.
26ondragoverscriptelement being over to target dragged when script tobe run.
27ondragstartscriptelement being start dragged when script tobe run.
28ondropscriptelement being dropped when script tobe run.
29onerrorscriptelement error occurs when script tobe run.
30onmessagescriptelement message display when script tobe run.
31onerrorscriptelement error occurs when script tobe run.
32onmousewheelscriptmouse wheel will be rotate when script tobe run.
33onscrollscriptscrollbar is scroll when script tobe run.
34onresizescriptelement should be resize when script tobe run.
35onselectscriptall element content selected when script tobe run.
36onstoragescriptelement should be store in target when script tobe run.

Browser Compatibility

SnoBrowserSupport
1ChromeYes
2FirefoxYes
3EdgeYes
4OperaYes
5SafariYes
6Internet ExplorerYes