The Wayback Machine - https://web.archive.org/web/20241001043203/https://www.geeksforgeeks.org/bootstrap-5-validation/
Open In App

Bootstrap 5 Validation

Last Updated : 01 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Bootstrap 5 Validation refers to the process of enhancing form input accuracy in web development. It utilizes HTML5 validation features, providing feedback to users regarding input correctness. This feature supports customization with custom styles and JavaScript for improved user experience.

Bootstrap 5 Validation:

TermDescription
Custom stylesAdd custom Bootstrap form validation messages by adding the novalidate boolean attribute to <form> an element. Disables default browser feedback tooltips.
Browser defaultsNo pre-defined class for feedback styles; cannot be styled with CSS. Feedback text can be customized via JavaScript.
Server-sideIndicate invalid and valid form fields with .is-invalid and .is-valid. Use .invalid-feedback and .valid-feedback for feedback messages.
Supported elements.form-control for <input> and <textarea>, .form-select for <select>, .form-check for checkboxes and radios.
TooltipsValidation tooltips provide interactive textual hints about form submission requirements.
SASSBootstrap 5 provides various SASS variables to validate form fields, ensuring accurate user-provided details.

Example 1: In this example, a form is created where the “Student Name”, “Roll no” and “Mobile Number” of the student needs to be submitted and by default this form will get validated by the browser using the required attribute.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <link href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
          rel="stylesheet">
</head>

<body class="m-3">
    <h1 class=text-success>
        GeeksforGeeks
    </h1>
    <form>
        <div>
            <label for="StudentName" 
                   class="form-label">
                Student Name
            </label>
            <input class="form-control" 
                   type="text" 
                   id="StudentName" required>
        </div>
        <div>
            <label for="rollNo" 
                   class="form-label">
                Roll No
            </label>
            <input class="form-control" 
                   type="text" 
                   id="rollNo" required>
        </div>
        <div>
            <label for="mobileNumber" 
                   class="form-label">
                Mobile Number
            </label>
            <input class="form-control" 
                   type="number"
                   id="mobileNumber" required /><br>
        </div>
        <div>
            <button class="btn btn-success" 
                   type="submit">
                Submit
            </button>
        </div>
    </form>
</body>

</html>

Output:

Example 2: In this example, we create Bootstrap form includes fields for name, email, password, gender, date of birth, and address. JavaScript validates password match, email format, gender selection, and address presence before submission.

HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0"
        />
        <title>Bootstrap Form</title>
        <link
            href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
            rel="stylesheet"
        />
    </head>
    <body>
        <div class="container">
            <div
                class="row justify-content-center mt-5"
            >
                <div class="col-md-6">
                    <form id="userForm">
                        <fieldset
                            class="border p-4 rounded mb-4 border-dark"
                        >
                            <legend
                                class="fs-5 mb-4"
                            >
                                User Personal
                                Information
                            </legend>
                            <div class="mb-3">
                                <label
                                    for="name"
                                    class="form-label"
                                    >Enter your
                                    full
                                    name</label
                                >
                                <input
                                    type="text"
                                    class="form-control"
                                    id="name"
                                    name="name"
                                    required
                                />
                            </div>
                            <div class="mb-3">
                                <label
                                    for="email"
                                    class="form-label"
                                    >Enter your
                                    email</label
                                >
                                <input
                                    type="email"
                                    class="form-control"
                                    id="email"
                                    name="email"
                                    required
                                />
                            </div>
                            <div class="mb-3">
                                <label
                                    for="password"
                                    class="form-label"
                                    >Enter your
                                    password</label
                                >
                                <input
                                    type="password"
                                    class="form-control"
                                    id="password"
                                    name="password"
                                    required
                                />
                            </div>
                            <div class="mb-3">
                                <label
                                    for="confirmPassword"
                                    class="form-label"
                                    >Confirm your
                                    password</label
                                >
                                <input
                                    type="password"
                                    class="form-control"
                                    id="confirmPassword"
                                    name="confirmPassword"
                                    required
                                />
                            </div>
                            <div class="mb-3">
                                <label
                                    class="form-label"
                                    >Enter your
                                    gender</label
                                >
                                <div class="row">
                                    <div
                                        class="col-md-4"
                                    >
                                        <div
                                            class="form-check"
                                        >
                                            <input
                                                class="form-check-input"
                                                type="radio"
                                                name="gender"
                                                id="male"
                                                value="male"
                                                required
                                            />
                                            <label
                                                class="form-check-label"
                                                for="male"
                                                >Male</label
                                            >
                                        </div>
                                    </div>
                                    <div
                                        class="col-md-4"
                                    >
                                        <div
                                            class="form-check"
                                        >
                                            <input
                                                class="form-check-input"
                                                type="radio"
                                                name="gender"
                                                id="female"
                                                value="female"
                                                required
                                            />
                                            <label
                                                class="form-check-label"
                                                for="female"
                                                >Female</label
                                            >
                                        </div>
                                    </div>
                                    <div
                                        class="col-md-4"
                                    >
                                        <div
                                            class="form-check"
                                        >
                                            <input
                                                class="form-check-input"
                                                type="radio"
                                                name="gender"
                                                id="others"
                                                value="others"
                                                required
                                            />
                                            <label
                                                class="form-check-label"
                                                for="others"
                                                >Others</label
                                            >
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label
                                    for="dob"
                                    class="form-label"
                                    >Enter your
                                    Date of
                                    Birth</label
                                >
                                <input
                                    type="date"
                                    class="form-control"
                                    id="dob"
                                    name="dob"
                                    required
                                />
                            </div>
                            <div class="mb-3">
                                <label
                                    for="address"
                                    class="form-label"
                                    >Enter your
                                    Address</label
                                >
                                <textarea
                                    class="form-control"
                                    id="address"
                                    name="address"
                                    rows="3"
                                    required
                                ></textarea>
                            </div>
                            <button
                                type="submit"
                                class="btn btn-primary"
                            >
                                Submit
                            </button>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
        <script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js">
        </script>
        <script>
            document
                .getElementById("userForm")
                .addEventListener(
                    "submit",
                    function (event) {
                        const password =
                            document.getElementById(
                                "password"
                            ).value;
                        const confirmPassword =
                            document.getElementById(
                                "confirmPassword"
                            ).value;
                        const email =
                            document.getElementById(
                                "email"
                            ).value;
                        const gender =
                            document.querySelector(
                                'input[name="gender"]:checked'
                            );
                        const address =
                            document.getElementById(
                                "address"
                            ).value;

                        if (
                            password !==
                            confirmPassword
                        ) {
                            alert(
                                "Passwords do not match"
                            );
                            event.preventDefault();
                        }

                        // Simple email validation
                        if (
                            !email.includes("@")
                        ) {
                            alert(
                                "Invalid email"
                            );
                            event.preventDefault();
                        }

                        // Gender validation
                        if (!gender) {
                            alert(
                                "Please select a gender"
                            );
                            event.preventDefault();
                        }

                        // Address validation
                        if (
                            address.trim() === ""
                        ) {
                            alert(
                                "Address field is required"
                            );
                            event.preventDefault();
                        }
                    }
                );
        </script>
    </body>
</html>

Output:



Previous Article
Next Article

Similar Reads

Bootstrap 5 Validation Server side
Bootstrap 5 Validation Server side provides valuable, actionable feedback to your users by browser default behaviors or custom styles and JavaScript. If you are using server-side validation, you can indicate invalid and valid form fields with .is-invalid and .is-valid and add .invalid-feedback & .valid-feedback with these classes it is a supported
3 min read
Bootstrap 5 Validation Tooltips
Bootstrap 5 Validation Tooltips are used to provide interactive textual hints to the user about the requirements to submit the form. Bootstrap 5 Validation Tooltips classes: valid|invalid-feedback: This class is used to tell the user about what things are necessary for the form to be submitted.valid|invalid-tooltip: This class is similar to the .{v
2 min read
How to Create a Bootstrap Form Validation ?
A Bootstrap Form Validation ensures that user input complies with established standards before submission, improves the user experience, and minimizes server-side processing for incorrect inputs. We can add client-side validation to their forms by using two different methods including Bootstrap built-in classes and JavaScript. Table of Content Usin
3 min read
How to Display Validation Message for Radio Buttons with Inline Images using Bootstrap?
By default, Bootstrap 4 has various form features for radio buttons with images inline. Here HTML 5 has default validation features, However, for custom validation, we must use JavaScript or jQuery. Below are the approaches that would help in displaying validation messages for radio buttons with images inline: Table of Content Using Inline ImageUsi
4 min read
Bootstrap 5 Validation Supported Elements
Bootstrap 5 Validation is supported by various elements present in the form and you can use it to make sure the user knows about which field is mandatory. The different elements in the form group which are supported are: <input>s and <textarea>s with .form-control<select>s with .form-select.form-checksBootstrap 5 Validation Suppor
3 min read
React Bootstrap Form Validation
In Web Application Development, React-Bootstrap is the framework that helps to develop web pages in a more attractive form. While developing the application, we designed input fields and forms that need to be validated to maintain security. So, we can do this by using React Bootstrap Form Validation. In this article, we will see the Syntax, Propert
3 min read
Bootstrap 5 Validation Browser defaults
Bootstrap5 Validation Browser defaults provide feedback by browser default behaviors. It mainly uses browser default when we do not want to provide validation feedback messages or are not interested in writing javascript code for validation then we can use browser defaults. Browser default validation depends on your OS. Bootstrap 5 Validation Brows
2 min read
Bootstrap 5 Validation Custom styles
Bootstrap 5 Validation Custom styles are used to add custom styles of bootstrap5 in your code. In order to add such custom styles we will have to add a novalidate boolean attribute to our <form>. After applying this attribute when you attempt to submit the form you can see the valid and invalid style of custom styles. Using custom styles we c
3 min read
Moment.js Parsing Validation
Date Validation in Moment.js moment#isValid is used to check whether a date is considered or not using Moment.js. This can be checked using moment#parsingFlags which returns an object in its response. Before proceeding, please install the moment.js library using the following command. Moment.js moment#parsingFlags that results in an invalid date: o
2 min read
AngularJS Form Validation
AngularJS performs form validation on the client side. AngularJS monitors the state of the form and input fields (input, text-area, select), and notify the user about the current state. AngularJS also holds information about whether the input fields have been touched, modified, or not. Form input fields have the following states: $untouched: It sho
3 min read
Laravel | Validation Rules
Validating the data coming from the user’s end is very important as it can help the user in filling proper data and prevent them from submitting any improper or malicious request. In Laravel, there are Validation Rules which are predefined rules, which when used in Laravel application. There is a list of rules that can be used to validate the data
5 min read
Form required attribute with a custom validation message in HTML5
The creation of web forms has perpetually been a fancy task. whereas marking up the shape itself is simple, checking whether or not every field features a valid and coherent price is tougher, and informing the user concerning the matter might become a headache. HTML5 introduced new mechanisms for forms. There 2 kinds of Constraint Validation DOM Me
4 min read
How to create form validation by using only HTML ?
In Web Development, we often use JavaScript with HTML to validate the form, but we can also do the same via HTML in the following ways. HTML <input> required Attribute HTML <input> type Attribute HTML <input> pattern Attribute HTML <input> required Attribute: In input tag of HTML, we can specify via "required attribute". It
2 min read
What is Unobtrusive Validation in jQuery?
jQuery is a Javascript library. An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side. Unobtrusive Validation means without writing a lot of validation code, you can perform simple client-side vali
2 min read
Spectre Forms Form validation styles
Spectre Forms validation styles class is used to style the form. To use form validation styles, you can either add is-success or is-error class to the controls. Add has-success or has-error class to parent elements. Use the form-input-hint class to provide form validation success and error messages. Forms Form validation styles Classes: is-success:
2 min read
Primer CSS Form Group Validation
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
3 min read
Struts2 Login Page with Validation
Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller architecture. In this article, we will see how we can create a login page with validations using Struts 2. We will use struts UI tags to create a login
6 min read
React Suite Form Validation Default Check
React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we’ll learn about React Suite Form Validation Default Check. A form is a set of components and mod
3 min read
How to apply validation on Props in ReactJS ?
Need of Validating Props in React JS Props are used to pass the read-only attributes to React components. For the proper functioning of components and to avoid future bugs and glitches it is necessary that props are passed correctly. Hence, it is required to use props validation to improve the react component's performance. Prerequisites:NodeJS or
3 min read
React.js Chakra UI Form Validation
Chakra UI is a simple and effective component-based library, that allows developers to build modern and attractive UI's for their website's frontends. Developers can simply use the components pre-defined in Chakra UI in their React.js Code to work faster and write less. The best part of Chakra UI is its reusability since it's a component-based libr
5 min read
React Suite Components Form validation
React Suite is a front-end library, containing a wide range of React Components used in enterprise applications. This article discusses how the React suite's Form Validation helps developers add validation to forms. Form Validation involves providing prompt information to the user when there is an error in the data entered with the form. Form valid
7 min read
Mongoose Validation
Mongoose is a MongoDB object modeling and handling for a node.js environment. Mongoose Validation is essentially a customizable middleware that gets defined inside the SchemaType of Mongoose schema. It automatically fires off before a document is saved in the NoSQL DB. Validation can also be run manually using doc.validate(callback) or doc.validate
3 min read
What is the purpose of controlled components in form validation?
Controlled components in form validation are like friendly assistants that help you keep track of what's happening in your form. Imagine them as little helpers who hold onto all the information you put into each input field. They ensure that every time you type something or change an entry, they carefully update and remember it for you. This not on
2 min read
Explain the use of the yup library for form validation.
The 'yup' library is a JavaScript library commonly used for schema validation, particularly in the context of form validation. Yup helps validate data against a predefined schema. It's often used in JavaScript applications, especially those built with frameworks like React, to ensure that the data entered into forms meets certain criteria or rules.
2 min read
How to implement validation in Express JS?
Validation is an important process in Express JS which ensures that the data received by the server fulfills the specific requirements or rules before it is processed further. This process helps in maintaining the integrity, security, and reliability of web applications. ExpressJS provides various tools and middleware, such as those used for valida
3 min read
How to handle data validation with React Hooks ?
Data validation is a critical component of software development that ensures the accuracy, consistency, and suitability of the data that is being processed or stored in a system. It involves implementing checks and constraints to ensure that the data meets certain criteria or standards. Key Points for Data Validation with React Hooks:useState Hook:
2 min read
PHP Form Validation
Form validation is a crucial step that needs to be completed before submitting data to the database. This process is performed to prevent any errors or faulty data from being inserted into the database. The HTML form includes various input fields such as email, text, checkbox, radio button, etc. These input fields must be validated to ensure that t
7 min read
How to perform custom validation in Angular forms?
Angular's built-in form controls provide a set of predefined validators for common validation such as required fields, email format, and minimum/maximum values. However, there may be cases where you need to implement custom validation logic specific to your application's requirements. Angular offers several approaches to accomplish this, allowing y
4 min read
How To Implement NextJS Form Validation With Formik And Zod ?
Form validation is a critical aspect of web development to ensure that user input meets specific criteria before submitting data to the server. Implementing form validation in Next.js can be simplified using Formik, a form library for React, and Zod, a schema validation library. Together, they provide a robust solution for managing form state and v
3 min read
Number validation in JavaScript
Sometimes the data entered into a text field needs to be in the right format and must be of a particular type in order to effectively use the form. For instance, Phone number, Roll number, etc are some details that must be in digits not in the alphabet. There are some apporaches: Table of Content isNaN() functionUsing typeof OperatorApproach 1: isN
2 min read