Is the styling of inline form elements compromised by validation in Bootstrap 4?

I'm facing an issue with validating an inline form field using Bootstrap 4.

Whenever the validation is triggered, the width of the inline form field jumps. You can see the problem in action by clicking Enter without entering a value on this fiddle: https://jsfiddle.net/aq9Laaew/213418/

Is there a way to resolve this?

Here's my HTML code:

<form class="form-inline needs-validation" id="form-stage1" novalidate>
  <label class="sr-only" for="stage1-amount">Amount in thousands</label>
  <div class="input-group mb-1 mr-sm-1">
    <div class="input-group-prepend"><div class="input-group-text">£</div></div>
    <input type="number" step="5000" min="0" class="form-control" id="stage1-amount" required>
  <div class="invalid-feedback">Please enter an amount to proceed</div>
  </div>
  <button type="submit" class="btn btn-secondary mb-1">Enter</button>
</div>
</form>

And here's my JavaScript code:

d3.select('#form-stage1').on('submit', function(e) { 
  d3.event.preventDefault();
  d3.event.stopPropagation();
  d3.select(this).attr('class', 'was-validated');
});

Answer №1

Give this a try:

    var form = document.getElementById('form-stage1');
    d3.select('#form-stage1').on('submit', function(e) { 
      d3.event.preventDefault();
      d3.event.stopPropagation();
      d3.select(this).attr('class', 'form-inline was-validated');
      $(".invalid-feedback").show();
      if (form.checkValidity() !== false) {
        d3.select('#stage1-results').style('display', 'inline-block');
        var val = d3.select('#stage1-amount').property("value");
        d3.select('#stage1-output').text(formatPounds(val));
        var percentile = getPercentile('property', val)
        d3.select('#stage1-lower').text(percentile);
        d3.select('#stage1-higher').text(100-percentile-1);
      }
    });
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}
.form-control{
  width:auto!important;
  flex:0!important;
}
.input-group{
  width:auto!important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap docs: https://getbootstrap.com/docs -->

<div class="container">
    <form class="form-inline needs-validation" id="form-stage1" novalidate>
  <label class="sr-only" for="stage1-amount">Amount in thousands</label>
  <div class="input-group mb-1 mr-sm-1">
<div class="input-group-prepend"><div class="input-group-text">£</div></div>
<input type="number" step="5000" min="0" class="form-control" id="stage1-amount" required>
  
  </div>
  <button type="submit" class="btn btn-secondary mb-1">Enter</button>
    <div class="invalid-feedback">Please enter an amount to proceed</div>
    </div>
    </form>
    
</div>

https://jsfiddle.net/aq9Laaew/215767/

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

How can we use JavaScript to close a dropdown menu when the user clicks outside of it?

I am facing an issue with my code. I want to close the dropdown menu when clicking outside of it or on the items within the dropdown. How can I achieve this? I attempted to use addEventListener('click', myFunction) on `document` but it did not w ...

What could be the reason behind the failure of my inner div to successfully implement the flex-row

I'm currently trying to position two divs next to each other horizontally, and my concept was to utilize flexbox, but including the property flex-row doesn't seem to yield any results and I can't comprehend the reason behind it. How can I a ...

What's the best way to remove the dead zone on a button with a relative position in HTML and CSS?

Recently, I've come across an issue with styled <a> and <button> tags. This problem arises when using display block or inline-block, some padding, and positioning relative to adjust the position on :active. a { display: inline-block; padd ...

Arrange the divs alongside one another within a container and ensure that the container adjusts its width to fit the children

I am in need of a container div: This div should adjust its width based on the content within. I would like the child elements to align horizontally. The container itself should form a single horizontal line. I prefer not to use flex because it tends to ...

jquery asynchronous image loading technique

Can images be loaded asynchronously while the page is loading? The images will only be displayed when a user clicks on a button, rather than immediately showing up. As a result, I am curious if it's feasible to preload the images in a cache so that th ...

tips for personalizing your jQuery image preview script

Currently, I have implemented a jQuery script that allows me to preview multiple images before uploading them. Although the functionality works well, I am facing difficulties customizing it to meet my specific requirements. <script> $(document).r ...

Different option for positioning elements in CSS besides using the float

I am currently working on developing a new application that involves serializing the topbar and sidebar and surrounding them with a form tag, while displaying the sidebar and results side by side. My initial attempt involved using flex layout, but I have ...

Is it possible to incorporate this design in a way that is responsive?

Can you share your insights on the best approach to implement this design in a responsive manner? I have a mockup ready for reference, featuring a lovely blue square. Here's the "mobile" version of the design. When the width is reduced to a point whe ...

Unique style pattern for parent links with both nested and non-nested elements

I am in the process of designing a website and I have a specific vision for how I want my links to appear, but I am unsure of how to achieve it. Here is the desired outcome: a red link a green link a red link a green link … Below is the HTM ...

What is the best way to incorporate animation using Tailwind CSS within my Next.js project?

I have a challenge with placing three images in a circular path one after the other using Tailwind CSS for styling. I've identified the circular path and keyframe style for the images. How do I configure this style in my Tailwind config file and imple ...

Building a grid layout using BootstrapWould you like to learn how

Could you please clarify the distinction between "test 1" and "test 2" when utilizing Bootstrap 4? I am attempting to design a layout that includes a small gutter between columns. One of them involves elements within the row, while the other does not. < ...

execute a jQuery function within a data list component

Looking to slide down a div and then slide it back up when a button is clicked. After searching on Google, I found a solution but unfortunately, the slide up function is not working as expected. When the button is clicked, the div slides down correctly but ...

Guide: Generating a DIV Element with DOM Instead of Using jQuery

Generate dynamic and user-defined positioning requires input parameters: offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth ...

What is the best way to ensure an SVG maintains a fluid width while keeping its height constant?

My goal is to achieve the following: The SVG should dynamically scale its width to occupy 100% of the container's width. The SVG should either stretch or compress when the container's width changes, specifically affecting the wave drawn wit ...

Chrome Table not behaving as expected when expanding div

I'm encountering an issue on my website where everything works perfectly in Firefox, IE, and Safari, but there is a glitch in Chrome. You can see the problem here: http://tinyurl.com/chxg2tb In Chrome, the table at the bottom extends beyond the cont ...

The source of the image gets disrupted when it is not on the homepage

My images are stored in the directory images/icons/artist_default.png On the homepage, the image is displayed with this path: http://localhost:7777/images/icons/artist_default.png However, on a user's page like http://localhost:7777/user/giorgio-m ...

Managing forms with Bootstrap

Hey there, I'm facing an issue with form handling in Bootstrap. Any suggestions to solve it? The problem lies with the spacing of elements, and unfortunately, I haven't been able to find a suitable solution yet. I've experimented with horiz ...

What is the best way to find out the size of a Google font file?

Google fonts are being utilized in my project, specifically the Roboto font for light and regular font styles. Unfortunately, I am unsure of the file size of this particular font. ...

Prevent modal from closing when clicking outside in React

I am currently working with a modal component in react-bootstrap. Below is the code I used for importing the necessary modules. import React from "react"; import Modal from "react-bootstrap/Modal"; import ModalBody from "react-bootstrap/ModalBody"; impor ...

What is the best way to display rows in alternating red and green colors?

Currently, I am implementing the react table in my React project. I found valuable resources on how to use it at these links: https://github.com/tannerlinsley/react-table/tree/v6#codesandbox-examples and also here: https://www.npmjs.com/package/react-tab ...