Skip to main content

Building Forms in HTML

Forms are an essential part of any website or web application. They allow users to interact with the website by providing input, such as text, selections, and buttons. Forms are used for various purposes, such as user registration, login, search, feedback, and more.

In this tutorial, you will learn how to create forms in HTML to collect user input effectively.

Creating a Simple Form​

To create a form in HTML, you need to use the <form> element. The <form> element is used to define an HTML form that collects user input. Here's an example of a simple form that collects the user's name and email address:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Form</title>
</head>
<body>
<h1>Simple Form</h1>
<form>
<label for="name">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>
<button type="submit">Submit</button>
</form>
</body>
</html>

In the example above:

  • We have created a simple form that collects the user's name and email address.
  • The <form> element contains two <input> elements for the name and email fields.
  • The type="text" attribute specifies that the input field is a text field.
  • The type="email" attribute specifies that the input field is an email field, which helps in validating the email address.
  • The required attribute specifies that the input field is required and must be filled out before submitting the form.
  • The <button> element with type="submit" is used to submit the form.

When the user submits the form by clicking the "Submit" button, the form data is sent to the server for processing. You can handle the form submission using JavaScript or server-side technologies like PHP, Node.js, etc.

Form Elements​

HTML provides various form elements that you can use to collect different types of user input. Some commonly used form elements include:

<input> Element​

The <input> element is used to create input fields within a form. It can be used to create text fields, checkboxes, radio buttons, buttons, and more. The type attribute of the <input> element specifies the type of input field to be created.

Here are some common input types:

  1. text: Creates a single-line text input field.

    For example:

    index.html
    <input type="text" name="username">
  2. password: Creates a password input field where the entered text is masked.

    For example:

    index.html
    <input type="password" name="password">
  3. email: Creates an email input field that validates the input as an email address.

    For example:

    index.html
    <input type="email" name="email">
  4. checkbox: Creates a checkbox input field that allows the user to select multiple options.

    For example:

    index.html
    <input type="checkbox" name="interest" value="coding"> Coding
    <input type="checkbox" name="interest" value="design"> Design
  5. radio: Creates a radio button input field that allows the user to select a single option from a list.

    For example:

    index.html
    <input type="radio" name="major" value="cs"> Computer Science
    <input type="radio" name="major" value="eng"> Engineering
  6. submit: Creates a submit button that submits the form data to the server.

    For example:

    index.html
    <input type="submit" value="Submit">
  7. button: Creates a button that can be used to trigger JavaScript functions.

    For example:

    index.html
    <button type="button" onclick="alert('Hello!')">Click Me</button>

<select> Element​

The <select> element is used to create a dropdown list within a form. It allows the user to select one or more options from a list of predefined options.

Here's an example of a <select> element with multiple options:

index.html
<select name="country">
<option value="us">United States</option>
<option value="ca">Canada</option>
<option value="uk">United Kingdom</option>
</select>

<textarea> Element​

The <textarea> element is used to create a multi-line text input field within a form. It allows users to enter multiple lines of text, such as comments or feedback.

Here's an example of a <textarea> element:

index.html
<textarea name="comments" rows="4" cols="50"></textarea>

In the example above, the rows and cols attributes specify the number of rows and columns of the textarea, respectively.

Form Validation​

Form validation is an essential part of creating forms to ensure that the user input is correct and complete. HTML provides built-in form validation features that can be used to validate user input without writing custom JavaScript code.

Here are some common form validation attributes that can be added to form elements:

  • required: Specifies that the input field is required and must be filled out.
  • minlength: Specifies the minimum length of the input field.
  • maxlength: Specifies the maximum length of the input field.
  • pattern: Specifies a regular expression pattern that the input field must match.
  • type: Specifies the type of input field (e.g., email, number, date).
  • min: Specifies the minimum value for number and date input fields.

Here's an example of a form with validation attributes:

index.html
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required minlength="3">
<br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required minlength="6">
<br><br>
<button type="submit">Submit</button>
</form>

In the example above:

  • The required attribute specifies that both the username and password fields are required.
  • The minlength="3" attribute specifies that the username must be at least 3 characters long.
  • The minlength="6" attribute specifies that the password must be at least 6 characters long.

When the user submits the form, the browser will automatically validate the input fields based on the specified attributes. If the input is invalid, the browser will display an error message to the user.

Key Components
  1. <form> Tag

    • Defines the start and end of a form.
    • Attributes:
      • action: URL where form data is sent.
      • method: HTTP method (e.g., GET or POST).
  2. Input Elements

    • Text Input (<input type="text">): Single-line text input.
    • Email Input (<input type="email">): Input validation for email addresses.
    • Textarea (<textarea>): Multi-line text input.
  3. Labels

    • The <label> element improves accessibility by associating text with form controls.
  4. Submit Button

    • <button> or <input type="submit"> submits the form.
  5. Validation Attributes

    • required, minlength, maxlength, pattern, etc.

Best Practices for Building Forms​

When building forms in HTML, it's essential to follow best practices to create user-friendly and accessible forms. Here are some best practices for building forms:

  • Use Labels: Always use the <label> element to associate text with form controls. This improves accessibility and makes it easier for users to understand the purpose of each input field.
  • Provide Feedback: Use validation attributes and error messages to provide feedback to users when they enter invalid data. This helps users correct their mistakes and complete the form successfully.
  • Group Related Fields: Group related form fields together using fieldsets and legends. This helps users understand the relationship between different fields and improves the overall form structure.
  • Use Placeholder Text: Use the placeholder attribute to provide hints or examples of the expected input in the form fields. Placeholder text disappears when the user starts typing.

By following these best practices, you can create forms that are easy to use, accessible, and user-friendly.

Conclusion​

In this tutorial, you learned how to create forms in HTML to collect user input effectively. You also learned about various form elements, form validation, and best practices for building forms.