Form validation: required, pattern, minlength, maxlength attributes : Module 3 Lesson 3

 Form validation is an essential part of web development, ensuring that users provide valid and complete data before submission. HTML5 provides built-in attributes such as required, pattern, minlength, and maxlength to help validate form inputs. This article will explain how to use these attributes, complete with code examples and visible outputs.

The required Attribute

The required attribute is used to indicate that an input field must be filled out before submitting the form. If a required field is left empty, the form will not be submitted, and the browser will display an error message.

Example

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Required Attribute Example</title> </head> <body> <h2>Required Attribute</h2> <form> <label for="username">Username (required):</label> <input type="text" id="username" name="username" required><br><br> <button type="submit">Submit</button> </form> </body> </html>

Output:


The pattern Attribute

The pattern attribute specifies a regular expression that the input field's value must match. It is useful for enforcing specific formats, such as phone numbers, postal codes, or custom validation rules.

Example

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pattern Attribute Example</title> </head> <body> <h2>Pattern Attribute</h2> <form> <label for="phone">Phone (format: 123-456-7890):</label> <input type="text" id="phone" name="phone" pattern="\d{3}-\d{3}-\d{4}" required><br><br> <button type="submit">Submit</button> </form> </body> </html>

Output:


The minlength and maxlength Attributes

The minlength and maxlength attributes specify the minimum and maximum number of characters allowed in an input field. These attributes ensure that the user inputs data within the required length range.

Example

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Minlength and Maxlength Attributes Example</title> </head> <body> <h2>Minlength and Maxlength Attributes</h2> <form> <label for="password">Password (5-10 characters):</label> <input type="password" id="password" name="password" minlength="5" maxlength="10" required><br><br> <button type="submit">Submit</button> </form> </body> </html>

Output:


Combining Validation Attributes

You can combine multiple validation attributes to enforce comprehensive input rules. Here’s an example that uses required, pattern, minlength, and maxlength together.

Example

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Combined Validation Attributes Example</title> </head> <body> <h2>Combined Validation Attributes</h2> <form> <label for="email">Email (required, valid format):</label> <input type="email" id="email" name="email" required><br><br> <label for="username">Username (5-15 characters, alphanumeric):</label> <input type="text" id="username" name="username" pattern="[A-Za-z0-9]{5,15}" required><br><br> <label for="password">Password (8-12 characters, at least one number):</label> <input type="password" id="password" name="password" pattern="(?=.*\d)[A-Za-z\d]{8,12}" required><br><br> <button type="submit">Submit</button> </form> </body> </html>

Output:


Conclusion

Using the required, pattern, minlength, and maxlength attributes in HTML forms helps ensure that users provide valid and complete data. These attributes are easy to implement and enhance the user experience by providing immediate feedback. By combining these attributes, you can create robust form validations that meet your specific requirements. Experiment with these attributes to create forms that are both user-friendly and secure

Previous Post Next Post