HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

HTML Input Attributes

The <input> tag is a versatile HTML element used in forms to collect user inputs. Depending on the attributes set, it can accept various types of data, from text to numbers and files.

Main Attributes of the <input> Tag

Attribute Description
type Specifies the type of input.
name Defines the name of the input element, which is used to reference form data after submission.
value Specifies the initial value of the input field.
placeholder Provides a hint to the user about what kind of information is expected in the input.
required Indicates that the input field must be filled out before submitting the form.
disabled Disables the input field.
pattern Defines a regex that the input field's value is checked against to ensure it matches a specified pattern.

Types of <input> and Examples

Type Description Example
text Allows the user to enter plain text. <input type="text" name="username" placeholder="Enter your username">
password Like text, but masks the user's input for privacy. <input type="password" name="password" placeholder="Enter your password">
radio Allows the user to select one of a limited number of choices. <input type="radio" name="gender" value="male" id="male"><label for="male">Male</label> <input type="radio" name="gender" value="female" id="female"><label for="female">Female</label>
checkbox Allows the user to select zero or more options from a limited number of choices. <input type="checkbox" name="subscribe" id="subscribe"><label for="subscribe">Subscribe to newsletter</label>
submit Button to submit the form. <input type="submit" value="Submit Form">
email Includes built-in validation to check if the entered text is a valid email address. <input type="email" name="email" placeholder="Enter your email">
date Allows the user to select a date. <input type="date" name="dob">
color Provides a color picker to choose a color. <input type="color" name="favoriteColor">

Using the pattern Attribute with Examples

The pattern attribute allows you to define a regular expression against which the input's value is checked. This is particularly useful for custom validations that go beyond the basic type checks.

Example: Validating a phone number (format: 123-456-7890):

<input type="text" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" title="Enter a phone number in the format: 123-456-7890" required>

This pattern ensures that the user enters a phone number exactly in the specified format. If the input does not match, the form will not submit, and the user will be prompted to correct the data.

Conclusion

The <input> tag is a critical element in HTML forms, providing a wide range of functionalities for gathering user inputs. Understanding how to use its various types and attributes, especially the pattern attribute for regex, can significantly enhance form handling and data validation in your web applications.