Bootstrap强制对带有表单验证的input-group元素进行四舍五入。

huangapple go评论59阅读模式
英文:

Bootstrap force rounding of input-group element with form validation

问题

我想将Bootstrap 5的input-groupform validation结合起来使用。然而,在验证消息存在时,最后的input-group元素是方形的,而不是圆形的。是否可能强制将其变为圆形?

奖励:是否可能为我的下面示例中的firstlast名称提供不同的验证消息

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
(() => {
  'use strict'
  const forms = document.querySelectorAll('.needs-validation')
  Array.from(forms).forEach(form => {
    form.addEventListener('submit', event => {
      if (!form.checkValidity()) {
        event.preventDefault()
        event.stopPropagation()
      }
      form.classList.add('was-validated')
    }, false)
  })
})()

<!-- language: lang-html -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>

<div class="container">
  <p/>
  <form class="row g-3 needs-validation" novalidate>
    <div class="col-md-6">
      <div class="input-group">
        <span class="input-group-text">First and last name</span>
        <input type="text" name="first" pattern="^\w{5}$" aria-label="First name" class="form-control" required>
        <input type="text" name="last" pattern="^\w{5}$" aria-label="Last name" class="form-control" required>
        <div class="invalid-feedback">
          upto 5 alphanumeric char only
        </div>
      </div>
    </div>
    <div class="col-12">
      <button class="btn btn-primary" type="submit">Submit form</button>
    </div>
  </form>
</div>

<!-- end snippet -->
英文:

I would like to combine Bootstrap 5's input-group with form validation. However, the last input-group element is squared and not rounded when validation message is present. Is it possible to force round it?

Bonus: is it possible to provide a different validation message for first & last name in my example below?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

(() =&gt; {
  &#39;use strict&#39;
  const forms = document.querySelectorAll(&#39;.needs-validation&#39;)
  Array.from(forms).forEach(form =&gt; {
    form.addEventListener(&#39;submit&#39;, event =&gt; {
      if (!form.checkValidity()) {
        event.preventDefault()
        event.stopPropagation()
      }
      form.classList.add(&#39;was-validated&#39;)
    }, false)
  })
})()

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;container&quot;&gt;
  &lt;p/&gt;
  &lt;form class=&quot;row g-3 needs-validation&quot; novalidate&gt;
    &lt;div class=&quot;col-md-6&quot;&gt;
      &lt;div class=&quot;input-group&quot;&gt;
        &lt;span class=&quot;input-group-text&quot;&gt;First and last name&lt;/span&gt;
        &lt;input type=&quot;text&quot; name=&quot;first&quot; pattern=&quot;^\w{5}$&quot; aria-label=&quot;First name&quot; class=&quot;form-control&quot; required&gt;
        &lt;input type=&quot;text&quot; name=&quot;last&quot; pattern=&quot;^\w{5}$&quot; aria-label=&quot;Last name&quot; class=&quot;form-control&quot; required&gt;
        &lt;div class=&quot;invalid-feedback&quot;&gt;
          upto 5 alphanumeric char only
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-12&quot;&gt;
      &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Submit form&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

你需要将.input-group类的<div>元素添加.has-validation类。

为了修复边框半径问题,输入组需要额外的.has-validation类。

<div class="input-group has-validation">
form.classList.add('was-validated')
英文:

You need to add the .has-validation class to the div with the .input-group class.

> To fix issues with border radius, input groups require an additional .has-validation class.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

(() =&gt; {
  &#39;use strict&#39;
  const forms = document.querySelectorAll(&#39;.needs-validation&#39;)
  Array.from(forms).forEach(form =&gt; {
    form.addEventListener(&#39;submit&#39;, event =&gt; {
      if (!form.checkValidity()) {
        event.preventDefault()
        event.stopPropagation()
      }
      form.classList.add(&#39;was-validated&#39;)
    }, false)
  })
})()

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;container&quot;&gt;
  &lt;br/&gt;
  &lt;form class=&quot;row g-3 needs-validation&quot; novalidate&gt;
    &lt;div class=&quot;col-md-6&quot;&gt;
      &lt;div class=&quot;input-group has-validation&quot;&gt;
        &lt;span class=&quot;input-group-text&quot;&gt;First and last name&lt;/span&gt;
        &lt;input type=&quot;text&quot; name=&quot;first&quot; pattern=&quot;^\w{5}$&quot; aria-label=&quot;First name&quot; class=&quot;form-control&quot; required&gt;
        &lt;input type=&quot;text&quot; name=&quot;last&quot; pattern=&quot;^\w{5}$&quot; aria-label=&quot;Last name&quot; class=&quot;form-control&quot; required&gt;
        &lt;div class=&quot;invalid-feedback&quot;&gt;
          upto 5 alphanumeric char only
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-12&quot;&gt;
      &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Submit form&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月13日 13:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定