Day 9 in Bootstrap - Coding Ranjith



Form Controls


Form Controls

In Bootstrap 5.3, there are various classes available to style and customize input boxes or form inputs. These classes allow you to apply predefined styles to your input elements, making them consistent and visually appealing. Here's an explanation of how to use input box classes in Bootstrap 5.3.

Form Controls in Bootstrap

Form Control

Sizing

Form text

Username must be 15 character [A-Z]

Disabled & Readonly

Plain Text

HTML File Input

Bootstrap File Input

Bootstrap Multiple File Input

Bootstrap Disabled File Input

Bootstrap File Input Sizing

HTML Color Input

Bootstrap Color Input

Datalist


Form Layouts


Form Layouts

In Bootstrap 5.3, you can create various form layouts using the grid system and predefined form classes. The grid system in Bootstrap allows you to divide the form into rows and columns, providing flexibility in arranging form elements. Here are a few common form layouts you can achieve in Bootstrap 5.3.

Basic Stack form Layout

In Bootstrap 5.3, you can create a basic stack form layout by applying the appropriate grid system and form classes. The stack layout will place form elements vertically on top of each other, providing a clean and organized form structure. Here's an example of a basic stack form layout in Bootstrap 5.3

Form Grid

In Bootstrap 5.3, you can create form layouts using the grid system to align form elements in a responsive manner. The grid system allows you to divide the form into rows and columns, providing flexibility in arranging and organizing form elements. Here's an example of how you can create a form grid layout in Bootstrap 5.3.

The form is divided into rows using the .row class, and the form elements are placed within columns created with the .col-* classes (e.g., .col-sm-6 for half width on small screens). The .mb-3 class adds margin-bottom spacing between rows, and the .mt-3 class adds margin-top spacing to the button row.

By utilizing the grid system, you can easily create a responsive form grid layout in Bootstrap 5.3, allowing your form elements to adapt to different screen sizes.

Horizontal Form Layout

Gender

Horizontal form Label Sizing

In Bootstrap 5.3, you can adjust the size of horizontal form labels using predefined classes. This allows you to control the visual presentation and alignment of labels within a horizontal form layout. Here's an explanation of horizontal form label sizing in Bootstrap 5.3

Bootstrap provides the following classes for horizontal form label sizing:


Floating Labels


Floating Labels

In Bootstrap 5.3, floating labels are a form design feature that provides an elegant and interactive way to display labels for form inputs. A floating label appears above the form input when the user interacts with it, such as clicking on the input field or entering text. The floating label then moves up and floats above the input, providing context and guidance to the user.

Floating Label in Bootstrap

Floating Label in Bootstrap

If value already defined label will automatically adjuct to floated Position

Form Validation Class

Textarea

Selects

Readonly plaintext

Input Groups

@
@
Please Enter Username

Form Validation


Form Validation

In Bootstrap 5.3, form validation is a powerful feature that allows you to validate user input and provide visual feedback to indicate whether the input is valid or not. Form validation can be implemented on the client-side using Bootstrap's built-in classes and JavaScript, providing a seamless user experience. Here's an explanation of form validation in Bootstrap 5.3

Client-side Validation:

Bootstrap 5.3 offers a range of CSS classes to facilitate client-side form validation. These classes are applied to form elements to indicate their validation state. The main classes are

Additionally, Bootstrap provides validation styles for feedback messages associated with form controls:

Required Fields:

Bootstrap 5.3 offers a range of CSS classes to facilitate client-side form validation. These classes are applied to form elements to indicate their validation state. The main classes are

Form Validation Classes:

Bootstrap 5.3 introduces new form validation classes that can be applied to form elements to trigger validation styling. These classes are:

Custom Validation:

Bootstrap 5.3 allows you to implement custom validation by adding the data-bs-validation attribute to form elements. You can define your own validation logic using JavaScript and apply custom validation styles accordingly.

Validation Feedback:

Bootstrap provides the .form-control-feedback class to display icons or other indicators of validation status alongside form controls.

Event Listener:

The code starts by adding an event listener to the form element with the ID "myForm". The event being listened to is the "submit" event, which is triggered when the user tries to submit the form.

Form Validation:

Inside the event listener, the code first checks if the form is valid using the form.checkValidity() method. If the form is not valid, the event's default behavior is prevented, and further propagation of the event is stopped.

Password Validation:

The code performs password validation by comparing the values of the password and confirmPassword input fields. If the passwords do not match, the setCustomValidity() method is called on the confirmPassword input element, setting a custom error message. Otherwise, the custom validity is cleared.

Phone Validation:

Phone number validation is performed using a regular expression (phoneRegex). The code checks if the phone number input value matches the regular expression. If it doesn't, the setCustomValidity() method is called on the phoneInput element to set a custom error message.

Pincode Validation:

Similar to phone validation, pincode validation is performed using a regular expression (pinRegex). The code checks if the pincode input value matches the regular expression. If it doesn't, the setCustomValidity() method is called on the pincodeInput element to set a custom error message.

Custom Date Validation:

The code performs custom validation for start and end dates. It compares the values of the startDate and endDate input fields, converting them into Date objects. If the start date is not before the end date, error messages are set using the setCustomValidity() method on both input fields.

Custom Validation for Select Field:

The code checks if a city has been selected in the select field with the ID "city". If no city is selected (i.e., the value is empty), a custom error message is set using the setCustomValidity() method.

Adding Validation Classes:

After performing all the validations, the code adds the "was-validated" class to the form element. This class is part of Bootstrap and triggers the display of validation styles and messages defined in the CSS.

Student Registration Form
Basic Information
Please enter your name.
Please select your gender.
Please enter a valid email address.
Please enter a password.
Passwords do not match.
Contact Information
Please enter a valid 10-digit phone number.
Please enter your address.
Please select a City.
Please enter a valid pincode.
Other Information
Start date must be before the end date.
End date must be after the start date.
You must agree to the terms and conditions.

--- Happy Coding ---