Introduction
Forms are an integral part of any web application. They allow users to input data, make selections, and interact with a website’s functionality. However, validating user input is crucial to ensure the data received is accurate and secure. This is where Bootstrap, a popular front-end framework, comes into play with its built-in form validation features. In this blog, we’ll explore how Bootstrap makes form validation more accessible and user-friendly.
Getting Started with Bootstrap Form Validation
Bootstrap simplifies form validation by providing a set of predefined CSS classes and JavaScript components. These tools allow developers to create forms with validation rules without having to write extensive custom code. Let’s start:
1. Include Bootstrap CSS and JavaScript
To use this feature, you must include the Bootstrap CSS and JavaScript files in your HTML document. You can do this by either downloading Bootstrap from the official website or using a content delivery network (CDN). Here’s an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Form Validation</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.5.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Your form goes here -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.5.0/dist/js/bootstrap.min.js"></script>
</body>
</html>
2. Create a Form
Next, you’ll need to create an HTML form using Bootstrap classes for styling. Bootstrap provides various form-related classes, such as .form-group
, .form-control
, and .form-check
. Here’s an example form:
<form>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
3. Add Validation Classes
Bootstrap offers validation classes that can be applied to form elements to define validation states. The most common ones are .is-valid
and .is-invalid
. These classes visually indicate whether the input is valid or not.
To trigger these classes dynamically, you can use JavaScript. Bootstrap provides JavaScript plugins that enable you to validate form inputs on user interaction.
Here’s an example of how to use the validation classes and JavaScript to validate an email input:
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" required>
<div class="invalid-feedback">
Please provide a valid email.
</div>
</div>
// JavaScript to validate the email input
document.getElementById('email').addEventListener('input', function () {
if (this.validity.valid) {
this.classList.remove('is-invalid');
this.classList.add('is-valid');
} else {
this.classList.remove('is-valid');
this.classList.add('is-invalid');
}
});
4. Customize Validation Feedback
You can provide custom feedback messages for each form element by adding <div>
elements with the class .invalid-feedback
inside the form group. These messages will be displayed when validation fails.
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" required>
<div class="invalid-feedback">
Please provide a valid email address.
</div>
</div>
Conclusion
Bootstrap makes form validation straightforward and user-friendly. By utilizing its built-in CSS classes and JavaScript components, you can create visually appealing and responsive forms with validation feedback. This simplifies the development process and enhances the user experience on your website.
Finally, for more such updates and to read more about such topics, please follow our LinkedIn page Frontend Competency