33. Form Validation Attributes
HTML allows us to define basic validation rules directly in the markup, which provides a fast, initial layer of client-side validation before the data even reaches the server.
33.1 The required Attribute
If present, the user must fill out the field before submitting the form. If left empty, the browser displays a standard error message.
html <label for="username">Username:</label> <input type="text" id="username" name="user" required>
33.2 Minimum and Maximum Length (minlength, maxlength)
Used for text inputs to define the acceptable number of characters.
html <input type="password" name="new_password" minlength="8" maxlength="20" required>
33.3 The pattern Attribute (Regular Expressions)
The pattern attribute allows you to specify a Regular Expression (RegEx) that the input value must match. This is used for complex validation, like zip codes or custom formats.
html <label for="zip">US Zip Code (5 digits):</label>
<!-- Pattern requires exactly 5 digits: [0-9] means any digit, {5} means exactly 5 times --> <input type="text" id="zip" name="zipcode" pattern="[0-9]{5}" title="Five digit zip code">Note: title is important here, as it provides a tooltip to the user explaining the required format.