Skip to content
Azril Hakim
LinkedInGitHub

Form & User Input

HTML forms are a crucial part of web development, enabling users to interact with web applications and submit data. In this section, we’ll explore the various form elements, learn how to perform form validation, and discover how to handle user input using JavaScript.

Form Elements

HTML provides several form elements that allow users to input data and make selections. Here are some of the most commonly used form elements:

<input>

The <input> element is versatile and can be used to create text input fields, radio buttons, checkboxes, and more. It’s essential for capturing user data.

Example of a text input field:

<input type="text" name="username" placeholder="Enter your username">

<textarea>

The <textarea> element is used for multiline text input. It’s suitable for capturing longer messages or comments from users.

Example of a textarea input field:

<textarea name="comments" rows="4" cols="50" placeholder="Enter your comments here"></textarea>

<select>

The <select> element creates dropdown menus, allowing users to choose from a list of options.

Example of a dropdown menu:

<select name="country">
  <option value="usa">United States</option>
  <option value="canada">Canada</option>
  <option value="uk">United Kingdom</option>
</select>

Form Validation

Form validation ensures that the data submitted by users is correct and meets specific criteria. HTML5 introduced built-in form validation attributes and JavaScript can be used for more advanced validation.

Built-in Validation

HTML5 provides attributes like required, min, max, pattern, and more, which can be added to form elements for basic validation.

Example of required field:

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

JavaScript Validation

For complex validation rules, JavaScript can be used to validate form data. Event listeners can be added to form elements to trigger validation functions.

Example of JavaScript form validation:

<form id="myForm">
  <input type="text" id="email" name="email">
  <span id="emailError" style="color: red;"></span>
  <button type="submit">Submit</button>
</form>

<script>
  const form = document.getElementById('myForm');
  const emailInput = document.getElementById('email');
  const emailError = document.getElementById('emailError');

  form.addEventListener('submit', function (event) {
    if (!isValidEmail(emailInput.value)) {
      emailError.textContent = 'Invalid email address';
      event.preventDefault(); // Prevent form submission
    }
  });

  function isValidEmail(email) {
    // Implement email validation logic here
    // Return true if valid, false otherwise
  }
</script>

Handling User Input with JavaScript

JavaScript is commonly used to enhance the functionality of HTML forms. You can use JavaScript to perform actions such as showing/hiding elements based on user input, dynamically updating form options, and handling form submissions asynchronously using AJAX.

Example of handling form submission with JavaScript:

<form id="myForm">
  <input type="text" name="username" id="username">
  <button type="button" onclick="submitForm()">Submit</button>
</form>

<script>
  function submitForm() {
    const username = document.getElementById('username').value;
    // Perform actions with user input
    // e.g., send data to the server via AJAX
  }
</script>

In this section, we’ve explored the fundamental aspects of HTML forms, including form elements, validation, and using JavaScript to handle user input. These skills are essential for creating interactive and user-friendly web applications.