DEV Community

Cover image for HTML Tutorials: Introduction to HTML Forms and Input Validation #5
Zahir Hadi Athallah
Zahir Hadi Athallah

Posted on • Edited on

HTML Tutorials: Introduction to HTML Forms and Input Validation #5

Introduction:

Welcome to Part 5 of our HTML tutorials! In this article, we will explore HTML forms and learn how to create interactive web forms for collecting user input. Forms are an essential element of many websites, enabling users to submit data, make selections, and interact with web applications. We will also cover input validation to ensure that the data entered by users meets specific criteria. Let's dive into HTML forms and input validation!

Creating an HTML Form:

To create an HTML form, we use the <form> tag. Within the form, we can add various input elements such as text fields, checkboxes, radio buttons, dropdown lists, and more.

Example:

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

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

    <input type="submit" value="Submit">
</form>
Enter fullscreen mode Exit fullscreen mode

In the example above, we have created a simple form with two input fields for name and email, along with a submit button.

Input Types and Attributes:

HTML provides different input types and attributes to control the behavior and validation of form inputs. Some commonly used input types include:

  • text: For single-line text input.
  • email: For email address input.
  • password: For password input (characters are masked).
  • checkbox: For multiple-choice selection.
  • radio: For single-choice selection.
  • select: For dropdown selection.

Example:

<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>

<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>

<select id="country" name="country">
    <option value="usa">USA</option>
    <option value="uk">UK</option>
    <option value="canada">Canada</option>
</select>
Enter fullscreen mode Exit fullscreen mode

In the example above, we have used radio buttons for gender selection and a dropdown select for country selection.

Input Validation:

Input validation is essential to ensure that users enter data in the correct format and meet specific requirements. HTML provides built-in form validation using attributes such as required, pattern, min, max, and more.

Example:

<label for="age">Age:</label>
<input type="number" id="age" name="age" required min="18" max="99">

<label for="password">Password:</label>
<input type="password" id="password" name="password" required minlength="8">
Enter fullscreen mode Exit fullscreen mode

In the example above, we have added validation to the age input, making it required and specifying minimum and maximum values. For the password input, we have set it as required and defined a minimum length.

Handling Form Submissions:

To handle form submissions, we can use server-side programming languages like PHP, Python, or JavaScript frameworks like Node.js. The form data can be sent to a server for processing and storing.

Example (JavaScript):

<script>
    function handleSubmit(event) {
        event.preventDefault(); // Prevents the form from submitting normally

        // Get form input values
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;

        // Perform further processing or data validation

        // Display success message
        alert("Form submitted successfully!");
    }
</script>

<form onsubmit="handleSubmit(event)">
    <!-- Form inputs go here -->
    <input type="submit" value="Submit">
</form>
Enter fullscreen mode Exit fullscreen mode

In the example above, we have added a JavaScript function to handle form submissions. The function prevents the form from submitting normally, retrieves the form input values, and performs further processing or validation.

Closing:

HTML forms are essential for collecting user input and interacting with web applications. In this tutorial, we explored creating HTML forms, using various input types and attributes, implementing input validation, and handling form submissions. By understanding these concepts, you can create interactive and user-friendly web forms that enhance the user experience. Keep experimenting and expanding your knowledge of HTML forms to build robust web applications. Happy coding!

Top comments (0)