HTML · Form Controls & Input Types · Lesson 11 of 32

Form Controls & Input Types

Form Controls & Input Types

HTML provides a variety of form controls that allow users to enter, select and submit information. The most commonly used form control is the <input> element.

The type of input control is determined using the type attribute.

Key Concept:

The <input> element is a versatile form control. Its type attribute determines what kind of data the user can enter.

1. Basic Text Input

The text input type allows the user to enter a single line of text.

Syntax

<input type="text" name="username">

Example

<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname">

It is commonly used for names, usernames, subjects, cities and other short text values.

Attribute Purpose
type Specifies the type of input control.
name Identifies the form field when form data is submitted.
id Provides a unique identifier for the element and can connect it with a label.
value Specifies an initial value for the control.

2. Password Input

The password input type is used to enter sensitive text such as passwords. Characters entered by the user are visually masked.

<label for="password">Password:</label>
<input type="password" id="password" name="password">
Important:

Password masking does not encrypt the data. Secure transmission requires appropriate security mechanisms such as HTTPS.

3. Email Input

The email input type is designed for entering an email address. Browsers can perform basic validation before the form is submitted.

<label for="email">Email:</label>
<input type="email" id="email" name="email">

Example

<form>

    <label for="email">Email Address:</label>
    <input type="email"
           id="email"
           name="email"
           required>

    <button type="submit">Submit</button>

</form>

The required attribute makes the field mandatory.

4. Number Input

The number input type is used when the user needs to enter a numerical value.

<label for="age">Age:</label>
<input type="number"
       id="age"
       name="age"
       min="1"
       max="100">
Attribute Purpose Example
min Specifies the minimum permitted value. min="1"
max Specifies the maximum permitted value. max="100"
step Specifies the permitted numerical interval. step="5"

5. Date Input

The date input type allows the user to select a date using a browser-provided date interface.

<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob">

Example

<label for="appointment">Appointment Date:</label>
<input type="date"
       id="appointment"
       name="appointment"
       min="2026-01-01">

6. Time Input

The time input type allows the user to enter or select a time.

<label for="meeting">Meeting Time:</label>
<input type="time" id="meeting" name="meeting">

7. Date and Time Input

The datetime-local input type allows users to enter both a date and a time.

<label for="event">Event:</label>
<input type="datetime-local"
       id="event"
       name="event">
Remember:

date → Date only

time → Time only

datetime-local → Date + Time

8. Radio Buttons

The radio input type allows the user to select one option from a group.

Radio buttons belonging to the same group must generally have the same name attribute.

<p>Select your preferred mode:</p>

<input type="radio"
       id="online"
       name="mode"
       value="online">
<label for="online">Online</label>

<input type="radio"
       id="offline"
       name="mode"
       value="offline">
<label for="offline">Offline</label>
Key Point:

Same name + different value = options in the same radio-button group.

9. Checkboxes

The checkbox input type allows the user to select zero, one or multiple options.

<p>Select your interests:</p>

<input type="checkbox"
       id="html"
       name="skills"
       value="html">
<label for="html">HTML</label>

<input type="checkbox"
       id="css"
       name="skills"
       value="css">
<label for="css">CSS</label>

<input type="checkbox"
       id="javascript"
       name="skills"
       value="javascript">
<label for="javascript">JavaScript</label>
Control Selection Typical Use
Radio Button Usually one option Gender, mode, payment method
Checkbox Multiple options Skills, interests, preferences

10. File Upload

The file input type allows the user to select a file from their device.

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

Restricting File Types

The accept attribute can suggest the types of files that should be selected.

<input type="file"
       name="image"
       accept="image/*">
Important:

File-type restrictions in HTML are not a complete security mechanism. A server should validate uploaded files independently.

11. Colour Picker

The color input type provides a control for selecting a colour.

<label for="theme">Choose Colour:</label>
<input type="color"
       id="theme"
       name="theme"
       value="#3366ff">

12. Range Slider

The range input type allows the user to select a numerical value from a specified range.

<label for="volume">Volume:</label>
<input type="range"
       id="volume"
       name="volume"
       min="0"
       max="100"
       value="50">

It is useful for controls such as volume, brightness, intensity and other numerical settings.

13. Search Input

The search input type is intended for entering search queries.

<label for="query">Search:</label>
<input type="search"
       id="query"
       name="query">

14. URL Input

The url input type is designed for entering a web address.

<label for="website">Website:</label>
<input type="url"
       id="website"
       name="website"
       placeholder="https://example.com">

15. Telephone Input

The tel input type is intended for telephone numbers.

<label for="phone">Phone Number:</label>
<input type="tel"
       id="phone"
       name="phone"
       placeholder="+1 555 123 4567">
Note:

The tel type does not automatically guarantee that the entered value is a valid telephone number. Additional validation may be required.

16. Hidden Input

The hidden input type stores a value that is not displayed as a normal form control to the user.

<input type="hidden"
       name="form_id"
       value="registration">
Security Warning:

A hidden field is not a secure storage mechanism. Its value can be viewed or modified by the user. Never use it to store passwords, secret keys or other sensitive information.

17. Submit and Reset Controls

Submit Button

A submit control sends the form data for processing.

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

Reset Button

A reset control restores the form controls to their initial values.

<input type="reset" value="Reset">

Button Type

The <button> element provides another flexible way of creating buttons.

<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button">Click Me</button>

18. Important HTML Input Types

Input Type Purpose Example Use
text Single-line text Name
password Masked text entry Password
email Email address Contact email
number Numerical value Age, quantity
date Date selection Date of birth
time Time selection Meeting time
datetime-local Date and time Appointment
radio Single choice from a group Payment method
checkbox Multiple selections Skills
file File selection Resume upload
color Colour selection Theme colour
range Numerical slider Volume
search Search query Website search
url Web address Personal website
tel Telephone number Contact number
hidden Non-visible form value Form identifier
submit Submit form Submit button
reset Reset form controls Reset button

19. Important Attributes of Form Controls

HTML provides several attributes that control the behaviour and validation of form controls.

Attribute Purpose Example
name Identifies submitted form data. name="email"
id Uniquely identifies an element. id="email"
value Defines the value associated with a control. value="Student"
placeholder Displays a temporary hint. placeholder="Enter name"
required Makes a field mandatory. required
readonly Prevents editing while allowing the value to remain visible. readonly
disabled Disables the form control. disabled
min Specifies minimum permitted value. min="1"
max Specifies maximum permitted value. max="100"
maxlength Limits the maximum number of characters. maxlength="50"
minlength Specifies the minimum number of characters. minlength="8"
pattern Defines a pattern for validation. pattern="[A-Za-z]+"
autocomplete Controls browser autocomplete behaviour. autocomplete="email"

20. The required Attribute

The required attribute tells the browser that the user must provide a value before the form can be submitted.

<label for="name">Name:</label>
<input type="text"
       id="name"
       name="name"
       required>
Remember:

required is a Boolean attribute. Its presence is enough; a value is not required.

21. placeholder vs value

Feature placeholder value
Purpose Provides a hint to the user. Specifies an actual initial value.
Submitted as data No Yes, if applicable.
Example placeholder="Enter name" value="Alex"
<input type="text"
       name="username"
       placeholder="Enter your username">

<input type="text"
       name="country"
       value="Canada">

22. readonly vs disabled

Feature readonly disabled
User can edit No No
Normally visible Yes Yes, but appears inactive.
Participates in form submission Generally yes No
Typical purpose Display a value without allowing editing. Temporarily make a control unavailable.

23. Labels and Accessibility

Every important form control should have an associated <label>. The for attribute of the label should match the id of the form control.

<label for="email">Email Address:</label>
<input type="email"
       id="email"
       name="email">
Accessibility Rule:

A label makes a form easier to understand and improves accessibility, especially for users of assistive technologies.

24. Complete Example Using Multiple Controls

The following example combines several input types in one registration form.

<form>

    <label for="name">Full Name:</label>
    <input type="text"
           id="name"
           name="name"
           required>

    <br><br>

    <label for="email">Email:</label>
    <input type="email"
           id="email"
           name="email"
           required>

    <br><br>

    <label for="password">Password:</label>
    <input type="password"
           id="password"
           name="password"
           required>

    <br><br>

    <label for="age">Age:</label>
    <input type="number"
           id="age"
           name="age"
           min="5"
           max="100">

    <br><br>

    <label for="dob">Date of Birth:</label>
    <input type="date"
           id="dob"
           name="dob">

    <br><br>

    <p>Preferred Learning Mode:</p>

    <input type="radio"
           id="online"
           name="mode"
           value="online">
    <label for="online">Online</label>

    <input type="radio"
           id="offline"
           name="mode"
           value="offline">
    <label for="offline">Offline</label>

    <br><br>

    <p>Skills:</p>

    <input type="checkbox"
           id="html"
           name="skills"
           value="html">
    <label for="html">HTML</label>

    <input type="checkbox"
           id="css"
           name="skills"
           value="css">
    <label for="css">CSS</label>

    <br><br>

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

    <br><br>

    <button type="submit">Register</button>
    <button type="reset">Clear</button>

</form>

25. Which Control Should You Use?

Requirement Recommended Control
Enter a person's name text
Enter a password password
Enter an email address email
Enter a numerical value number
Select one option radio
Select multiple options checkbox
Select a date date
Select a time time
Upload a document file
Select a colour color
Select a value using a slider range
Enter a website address url
Enter a telephone number tel
Search for information search

Case Study: Online Course Registration

Think Like a Web Developer

An online learning platform needs a registration form that collects the learner's name, email address, password, age, preferred learning mode and technical skills.

The developer should use a text input for the name, email for the email address, password for the password, number for age, radio buttons for the learning mode and checkboxes for multiple skills.

The form should also use appropriate labels and the required attribute for fields that must be completed.

Common Beginner Mistakes

  • Using text when a more appropriate input type such as email or number is available.
  • Giving radio buttons different name attributes when they are supposed to form one group.
  • Forgetting to associate a label with its form control.
  • Assuming that placeholder replaces a proper label.
  • Treating hidden fields as secure storage.
  • Depending only on client-side validation for security.
  • Forgetting the name attribute when form data needs to be submitted.
  • Using checkboxes when only one option should be selected.
  • Using radio buttons when users should be able to select multiple options.

Exam Questions

Question 1: Which input type is used to accept a password?

Click to View Answer

The password input type is used.

Question 2: Which input type should be used when a user can select only one option from a group?

Click to View Answer

radio input type.

Question 3: Which input type allows users to select multiple options?

Click to View Answer

checkbox input type.

Question 4: What is the purpose of the required attribute?

Click to View Answer

It makes a form control mandatory. The browser prevents normal form submission until a valid value is provided.

Question 5: Differentiate between radio buttons and checkboxes.

Click to View Answer

Radio buttons are generally used to select one option from a group, whereas checkboxes allow the user to select multiple independent options.

Question 6: What is the purpose of the name attribute in a form control?

Click to View Answer

The name attribute identifies the form field and is used as the field name when its data is submitted.

Question 7: What is the difference between placeholder and value?

Click to View Answer

placeholder provides a temporary hint to the user, whereas value specifies the actual initial value of the control.

Question 8: Which input type is appropriate for uploading a file?

Click to View Answer

The file input type.

Question 9: Which attribute connects a label with a form control?

Click to View Answer

The label's for attribute should match the id of the form control.

Interview Questions

1. Why are different input types available in HTML?

Click to View Answer

Different input types allow developers to communicate the expected kind of data to the browser and provide an appropriate user interface and basic validation.

2. Why should radio buttons have the same name?

Click to View Answer

Radio buttons with the same name are treated as one group, allowing the user to select one option from that group.

3. Is client-side validation enough to secure a form?

Click to View Answer

No. Client-side validation improves user experience, but server-side validation is also required because client-side controls can be bypassed or manipulated.

4. Is a hidden input secure?

Click to View Answer

No. Hidden inputs are not security mechanisms. Their values can be inspected and modified by the user.

5. Why is the email input type better than text for an email address?

Click to View Answer

The email type communicates that an email address is expected and enables browser-level validation and appropriate input behaviour on supported devices.

Practice Activity

Create an HTML registration form for an online learning platform using the following controls:

  • Full Name
  • Email Address
  • Password
  • Age
  • Date of Birth
  • Preferred Learning Mode using radio buttons
  • Programming Skills using checkboxes
  • Resume Upload
  • Submit and Reset buttons
Challenge:

Use appropriate labels, name and id attributes, and apply suitable validation attributes such as required, min and max.

Quick Revision

  • <input> is a versatile HTML form control.
  • The type attribute determines the kind of input control.
  • text is used for single-line text.
  • password masks entered characters.
  • email is intended for email addresses.
  • number is used for numerical values.
  • date, time and datetime-local handle date/time input.
  • radio is generally used for one choice from a group.
  • checkbox allows multiple selections.
  • file allows file selection.
  • range provides a numerical slider.
  • required makes a field mandatory.
  • placeholder provides a hint; it is not a replacement for a label.
  • name identifies submitted form data.
  • A label's for attribute should match the control's id.
  • Client-side validation should not replace server-side validation.
Memory Trick: Choose the Input by the Data

Text → text   |   Secret → password   |   Email → email

Number → number   |   One Choice → radio   |   Multiple Choice → checkbox

Date → date   |   File → file   |   Slider → range