Skip to content

Forms and Inputs— Forms Advanced

  1. HTML5 Forms Advanced
  • New Input Types (email, date, range, etc.)
  • Overview of new input types and their uses
  • Form Validation Using HTML5
  • Adding advanced validation rules

HTML5 Forms: New Input Types and Form Validation

Section titled “HTML5 Forms: New Input Types and Form Validation”

HTML5 introduced several new input types and validation features that make it easier to create forms that provide a better user experience and reduce the need for custom JavaScript validation.

HTML5 added a variety of new input types to enhance forms by offering specific fields for different kinds of data. These input types help ensure users provide the correct type of information and can enhance the usability and accessibility of web forms.

  1. Email (<input type="email">): Validates that the input is a valid email address format.
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
  1. URL (<input type="url">): Ensures the input is a valid URL format.
<label for="website">Website:</label>
<input type="url" id="website" name="website">
  1. Telephone (<input type="tel">): For entering phone numbers, though it does not validate the format.
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone">
  1. Number (<input type="number">): For numeric input with optional attributes to define range and step.
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" max="100" step="1">
  1. Range (<input type="range">): For selecting a value from a range of numbers using a slider.
<label for="volume">Volume:</label>
<input type="range" id="volume" name="volume" min="0" max="100" step="1">
  1. Date (<input type="date">): For selecting a date using a date picker.
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
  1. Time (<input type="time">): For selecting a time.
<label for="appointment">Appointment Time:</label>
<input type="time" id="appointment" name="appointment">
  1. Datetime-local (<input type="datetime-local">): For selecting both date and time, without timezone information.
<label for="meeting">Meeting Date and Time:</label>
<input type="datetime-local" id="meeting" name="meeting">
  1. Month (<input type="month">): For selecting a month and year.
<label for="start">Start Month:</label>
<input type="month" id="start" name="start">
  1. Week (<input type="week">): For selecting a specific week of a year.
<label for="week">Week:</label>
<input type="week" id="week" name="week">
  1. Color (<input type="color">): For selecting a color using a color picker.
<label for="favcolor">Favorite Color:</label>
<input type="color" id="favcolor" name="favcolor" value="#ff0000">

HTML5 introduces built-in form validation features that make it easier to validate user input without relying on JavaScript. These features include various attributes that define validation rules directly in the HTML.

  1. Required Attribute (required): Ensures an input field must be filled out before submitting the form.
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
  1. Pattern Attribute (pattern): Specifies a regular expression that the input value must match.
<label for="zipcode">Zip Code:</label>
<input type="text" id="zipcode" name="zipcode" pattern="[0-9]{5}" required>
  1. Min and Max Attributes (min, max): Define the minimum and maximum values for numeric inputs.
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="0" max="100" required>
  1. Minlength and Maxlength Attributes (minlength, maxlength): Specify the minimum and maximum number of characters for text inputs.
<label for="password">Password:</label>
<input type="password" id="password" name="password" minlength="8" maxlength="20" required>
  1. Step Attribute (step): Specifies the legal number intervals for numeric and range inputs.
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="0" max="100" step="1">
  1. Title Attribute (title): Provides a tooltip that appears when the user hovers over the input field, often used to give hints about the expected input format.
<label for="username">Username:</label>
<input type="text" id="username" name="username" required title="Username should be alphanumeric.">
  1. Custom Error Messages: Use JavaScript to provide custom error messages if the built-in validation messages are not sufficient.
<script>
document.getElementById("myForm").addEventListener("submit", function(event) {
var emailField = document.getElementById("email");
if (!emailField.validity.valid) {
emailField.setCustomValidity("Please enter a valid email address.");
} else {
emailField.setCustomValidity("");
}
});
</script>
  1. Validation Pseudo-Classes: Use CSS pseudo-classes to style inputs based on their validation state.
input:valid {
border: 2px solid green;
}
input:invalid {
border: 2px solid red;
}

Example Form with HTML5 Validation:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5 Form Validation</title>
<style>
input:valid {
border: 2px solid green;
}
input:invalid {
border: 2px solid red;
}
</style>
</head>
<body>
<form id="myForm" action="/submit" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" minlength="8" maxlength="20" required>
<br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="0" max="100" step="1">
<br>
<label for="website">Website:</label>
<input type="url" id="website" name="website">
<br>
<input type="submit" value="Submit">
</form>
<script>
document.getElementById("myForm").addEventListener("submit", function(event) {
var emailField = document.getElementById("email");
if (!emailField.validity.valid) {
emailField.setCustomValidity("Please enter a valid email address.");
} else {
emailField.setCustomValidity("");
}
});
</script>
</body>
</html>

By leveraging HTML5 input types and validation features, you can create forms that are user-friendly, accessible, and require less client-side scripting to enforce data integrity.