What is form tag in HTML with Example? - onlyxcodes

Monday 26 August 2024

What is form tag in HTML with Example?

In this article, I will dive deep into what the <form> tag is, its attributes, and how it can be utilized with examples to enhance your website’s functionality.


An integral part of HTML, the <form> tag forms the foundation of many web applications. Developers can quickly gather and handle user input by using the <form>tag in everything from basic contact forms to comprehensive registration and payment systems.


what is form tag in html with example

What is the <form> Tag?

The <form> tag in HTML is used to create an interactive form that allows users to submit data to a web server. The data is transmitted to the server for processing when a user completes the form and hits the submit button. Most online interactions, such as order submissions, feedback forms, and login systems, are built on top of this.


Basic Structure of a Form

Here is a simple example of a basic HTML form:


In this example, the form collects a user's name and email address and then submits it to the server for processing.


<form action="/submit-form" method="post">
  <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>

Key Attributes of the <form> Tag

To use the <form> tag effectively, it is crucial to understand its key attributes:


action: Where the form data should be routed after submission is specified by the action attribute. Usually, an endpoint or a server-side script is used to process the data.


In this example, the form data will be sent to the /submit-form URL.


<form action="/submit-form" method="post">

method: The method attribute determines how the form data is sent to the server. The two most common methods are GET and POST.

  • GET: adds name/value pairs of the form data to the URL. This method works well for searches and data retrieval.

  • POST: Sends the form data as a separate HTTP message. This method is more secure and is typically used for submitting sensitive information like passwords.

In the example, the POST method is used to submit the form data.


<form action="/submit-form" method="post">

enctype: When file uploads are present on the form, the enctype property is used. This attribute's most popular value is multipart/form-data, which permits the submission of file data together with the form.


In this example, the form allows users to upload files to the server.


<form action="/upload" method="post" enctype="multipart/form-data">

name: The name attribute is used to identify the form. While not always necessary, it can be helpful for scripting and form management.


In this example, the form is given the name userForm.


<form name="userForm" action="/submit-form" method="post">

Form Elements

Forms consist of various input elements that allow users to enter data. These elements include:


<input>: The <input> element is the most common form control. It can create text fields, checkboxes, radio buttons, and more.


In this example, I created a single-line text input field.


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

<textarea>: The <textarea> element is used for multi-line text input, such as comments or messages.


In this example, I have created a larger text input area.


<textarea id="message" name="message"></textarea>

<select>: The <select> element creates a drop-down list. It is used when you want users to select one option from a list.


In this example, I have used a drop-down menu to allow users to select their country.


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

<button>: The <button> element creates a clickable button, which can be used for submitting forms or triggering JavaScript actions.


In this example, I used the submit button when I clicked the form data.


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

<label>: The <label> element is used to define labels for input elements, improving accessibility and user experience.


In this example, I have used a tag that associates the label with a specific input field.

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

Form Validation

A web form's form validation is a necessary component. It verifies that users provide accurate data and aids in avoiding mistakes made when processing data. HTML offers many characteristics and methods for validating forms.


required: The required attribute ensures that a user cannot submit the form without filling out the specified field.


In this example, I have taken the input type name field, and in this field, I applied "required" validation because this field must be filled out before submission.


<input type="text" id="name" name="name" required>

pattern: The pattern attribute allows you to define a regular expression that the input must match.


In this example, I have used the input field that requires a 10-digit phone number.


<input type="text" id="phone" name="phone" pattern="[0-9]{10}">

min and max: These attributes are used with several input fields to set the minimum and maximum values that can be entered.


In this example, I have used one field and applied min and max attributes that only accept numbers between 18 and 99.


<input type="number" id="age" name="age" min="18" max="99">

Enhancing Forms with JavaScript

While HTML provides basic validation, JavaScript can be used to create more dynamic and responsive forms. JavaScript can be employed to:

  • Provide real-time validation as users fill out the form.

  • Show or hide fields based on user input.

  • Submit forms asynchronously using AJAX.

Here’s a simple example of using JavaScript for real-time validation:


In this example, the form will alert the user if they enter an invalid email address before allowing submission.


<script>
  function validateEmail() {
    var emailField = document.getElementById("email");
    var email = emailField.value;
    var regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    
    if (!regex.test(email)) {
      alert("Please enter a valid email address.");
      emailField.focus();
      return false;
    }
    return true;
  }
</script>

<form onsubmit="return validateEmail()">
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
  <input type="submit" value="Submit">
</form>

Wrapping Up


Read Also:

How to Insert Background Image in HTML from Local Folder

What is the difference between P and pre in HTML?

How to set value in span tag in HTML

What is the video tag in HTML?

What is the HR and BR tag in HTML?

No comments:

Post a Comment

Post Bottom Ad