Tips for getting validation to function properly on Bootstrap v4 using Bootstrap validator

Recently, I made the switch from Bootstrap v3 to v4, and although the JavaScript part of Bootstrap Validator is still functioning properly, the styling seems to have disappeared.

The bootstrap validator that I utilized can be found here.

This is how it currently looks: https://i.sstatic.net/Nyc4j.jpg

Previously, it appeared like this: https://i.sstatic.net/lXt5X.jpg

To ensure that error messages are displayed correctly, I manually added the CSS. However, the label highlight and input box highlight do not seem to be working as expected when an error occurs.

All classes have been converted according to BS4 in my code:

$('#inputFirstName').attr('data-required-error', uiData.Error_FieldRequired_FirstName);
$('#inputLastName').attr('data-required-error', uiData.Error_FieldRequired_LastName);
$('#inputPhone').attr('data-required-error', uiData.Error_FieldRequired_Phone);
$('#inputEmail').attr('data-required-error', uiData.Error_FieldRequired_Email);
$('#inputTitle').attr('data-required-error', uiData.Error_FieldRequired_Title);

$('#inputPhone').attr('data-pattern-error', uiData.Error_InvalidPhone);
$('#inputEmail').attr('data-pattern-error', uiData.Error_InvalidEmail);
$('#inputEmail').attr('data-match-error', uiData.Error_InvalidEmail);
.help-block {
    color: red;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<form data-toggle="validator" role="form" id="userForm">
  <div class="form-group">
    <label for="inputFirstName" class="form-control-label">First name</label>
    <input type="text" class="form-control" id="inputFirstName" value="" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputLastName" class="form-control-label">Last name</label>
    <input type="text" class="form-control" id="inputLastName" value="" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputPhone" class="form-control-label">Phone number</label>
    <input type="tel" class="form-control" id="inputPhone" pattern="\d{3}\.\d{3}\.\d{4}" value="" data-inputmask="'mask': '999.999.9999', 'showMaskOnFocus': 'false', 'showMaskOnHover': 'false'" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputEmail" class="form-control-label">Email address</label>
    <input type="email" class="form-control" id="inputEmail" pattern="^[a-zA-Z0-9._+-]+@(" @ ")[a-zA-Z0-9.-]+\.[a-zA-Z]{2,9}$" value="" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputTitle" class="control-label">Position or title</label>
    <input type="text" class="form-control" id="inputTitle" required>
    <div class="help-block with-errors"></div>
  </div>
</form>

Thank you for your help!

Answer №1

Firstly, ensure that your script files are in the correct order. jQuery should be placed at the top and avoid using bootstrap-validator multiple times unnecessarily.

To address your issue, you need to apply the has-danger class to style the label.

.form-group.has-error.has-danger .form-control-label {
  color: red;
}
.form-group.has-error.has-danger .form-control{
  border: 1px solid red;
  box-shadow: 0 0 0 0.2rem rgba(250, 16, 0, 0.18);
}

Stack Snippet

$('#userForm').validator();
.help-block {
  color: red;
}

.form-group.has-error.has-danger .form-control-label {
  color: red;
}

.form-group.has-error.has-danger .form-control {
  border: 1px solid red;
  box-shadow: 0 0 0 0.2rem rgba(250, 16, 0, 0.18);
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<form data-toggle="validator" role="form" id="userForm">
  <div class="form-group">
    <label for="inputFirstName" class="form-control-label">First name</label>
    <input type="text" class="form-control" id="inputFirstName" value="" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputLastName" class="form-control-label">Last name</label>
    <input type="text" class="form-control" id="inputLastName" value="" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputPhone" class="form-control-label">Phone number</label>
    <input type="tel" class="form-control" id="inputPhone" pattern="\d{3}\.\d{3}\.\d{4}" value="" data-inputmask="'mask': '999.999.9999', 'showMaskOnFocus': 'false', 'showMaskOnHover': 'false'" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputEmail" class="form-control-label">Email address</label>
    <input type="email" class="form-control" id="inputEmail" pattern="^[a-zA-Z0-9._+-]+@(" @ ")[a-zA-Z0-9.-]+\.[a-zA-Z]{2,9}$" value="" required>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="inputTitle" class="form-control-label">Position or title</label>
    <input type="text" class="form-control" id="inputTitle" required>
    <div class="help-block with-errors"></div>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The dropdown menu is displaying the initial value from the JSON file rather than the expected default text

Operation.. Users need to choose the items displayed in a dropdown menu, which are read from a JSON file. The dropdown menu should initially show a default text of "Please select shops..." before displaying the item names. Challenge: The default text of ...

What are the best methods to activate grayscale and transition effects on Firefox and Internet Explorer?

In order to achieve a grayscale effect for all images on Internet Explorer 10 and IE 11, I came across a helpful solution in this post: internet explorer 10 - howto apply grayscale filter?. However, the method provided is only for a single image. How can I ...

JavaScript - Attempting to insert a value into a text field by clicking a button

I am struggling to get my function to properly add the value of the button to the text field when clicked by the user. HTML <form name="testing" action="test.php" method="get">Chords: <textarea name="field"& ...

Automatically synchronize dynatable with AJAX requests and JSON responses

I'm currently facing a bit of confusion as my code appears to be functioning properly, but the table is not updating as expected. xD I am fetching data from my database and loading it into a table, aiming for it to automatically update every three se ...

If the font size exceeds a certain value in SCSS, then apply a different value

Can SCSS handle this scenario? I am looking to set the font size to 22px in case it is greater than 30px, especially within a media query for a max width of 480px. ...

How to retrieve a BLOB image and incorporate it into an HTML webpage with PHP

I am having trouble retrieving an image from a MySQL database and displaying it on a web page. I have a user_image table with columns for userid, image (stored as blob datatype), caption, imagename (name of the image, e.g., 0101.jpg), and time (timestamp ...

What is the method for incorporating personalized CSS into crispy forms?

Trying to design a responsive form for my website using crispy forms without the use of bootstrap. I want to include custom CSS to ensure the form matches the overall look of my website. HTML: <form method='POST' action=''>{% cs ...

Activating a div in React.js using setActive: Step-by-step guide

Currently, I am in the process of developing an application with three menu tabs, each represented by an accordion comprising a menu title and content. The issue I am facing is that when clicking on any menu title, all content divs are displayed simultaneo ...

Email link with attached file

For my client, I developed an application that delivers a zip file with the following structure: index.html /files/ file.pdf /inc/ style.css The purpose of this app is to allow users to transfer the zip file to their iPad or iPhone using Sites- ...

View box 2 from the opposite side of box 1

I am looking for a way to make box2 appear behind box1. One solution is to use position: relative; in the .box1 class. However, this causes another issue in my project. Is there a way to display .box2 behind .box1 without using position: relative; in .b ...

Troubleshooting: Issue with AJAX xmlhttp.send() functionality

I'm new to AJAX and have been stuck on the same issue for hours. Here is my script code: <script language='javascript'> function upvote(id ,username) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = fun ...

Ajax continues to operate even if an error is detected

Hello fellow programmers! I'm currently facing an issue with form submission and validation using jQuery with 2 ajax events. The problem lies in the fact that even if the first ajax event (timeconflict.php) returns an error, the second ajax event (res ...

Unable to assign attribute following discovery

Can the attribute of an anchor element that is found using find() be set? I attempted this: $(this).children('a').setAttribute("href","a link"); Although it does locate the anchor element, why am I receiving an error when trying to use setAttr ...

How can I retrieve the duration of a video using the YouTube Data API?

Essentially, I am attempting to extract the video duration for each video that appears in the search results. Feel free to play around with the demo I have prepared! Additionally, when I refer to duration, I am talking about the length of the video in M/S ...

Looking to design a popup using CSS styles

How can I design a pop-up window using CSS for a specific div? I also want to display additional information within another div inside the popup. ...

Are external JavaScript files cached in the same manner as images?

Is it possible for a single pagehandler script to be called from the browser cache when navigating between multiple pages that refer to it? ...

column with medium width of 4 is displaying oversized content

Hi there, I'm new to the forum and also to bootstrap. I have a problem with my layout that I can't quite describe (I am thinking in Spanish). Here's what's going on: I have three col-md-4 columns: the first column contains only text, w ...

Adding LocalStorage functionality to buttons in order to switch colors can be done by storing the

I've run into an issue trying to incorporate LocalStorage with the buttons that change colors as themes in JavaScript. I'm new to coding and eager to add this feature to my personal blog. $(document).ready(function () { $(".header--theme-button ...

Switch back and forth between `display: none` and `display: flex` using JavaScript

There is a JavaScript function in place for a responsive navigation system that includes a burger button to toggle the visibility of the navigation menu when the screen size is too small. The issue I am facing is that despite setting the CSS style as disp ...

Standard User Interface Designs

I need to showcase a web app prototype to a client in the upcoming days. However, I struggle with CSS and never seem satisfied with the outcomes I achieve. While coding the business logic is easy for me, designing the UI consumes most of my time. I prefer ...