无法在 Bootstrap 中进行验证 [英] Can't make the validation work in Bootstrap

查看:44
本文介绍了无法在 Bootstrap 中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施 通过 Bootstrap 验证,我已经在我的页面上粘贴了以下示例:

<label class="form-control-label" for="inputSuccess1">输入成功</label><input type="text" class="form-control form-control-success" id="inputSuccess1"><div class="form-control-feedback">成功!你已经做到了.</div><small class="form-text text-muted">保持不变的帮助文本示例.</small>

我可以看到输入控件的外观发生了变化(现在有点圆润,更美观)但是它仍然没有显示页面上可以看到的绿色边框链接到.我链接到的 Bootstrap 指出如下.

我曾尝试用谷歌搜索此问题,但无济于事.我有一个说明问题的小提琴.

我该怎么办?我错过了什么?

解决方案

Bootstrap 5(2021 年更新)

由于 Bootstrap 5 不再需要 jQuery,因此很容易使用 vanilla JavaScript 进行客户端验证.文档包括一个通用代码示例,它应该适用于所有具有 needs-validation..

的表单

(函数(){'使用严格'//获取我们想要应用自定义 Bootstrap 验证样式的所有表单var forms = document.querySelectorAll('.needs-validation')//遍历它们并防止提交Array.prototype.slice.call(表单).forEach(函数(形式){form.addEventListener('submit', function (event) {如果(!form.checkValidity()){event.preventDefault()event.stopPropagation()}form.classList.add('已验证')}, 错误的)})})()

Bootstrap 5 表单验证演示

引导程序 4(原始答案)

Bootstrap 4 测试版起,验证发生了变化.

有效状态选择器使用 was-validated 类,该类将在通过 客户端 JavaScript 验证表单后动态添加.例如...

<div class="valid-feedback">成功!你已经做到了.</div>

<div class="form-group"><label class="form-control-label";for=inputSuccess2">输入有危险</label><输入类型=文本"类=表单控制"名称=i2"必需="id="inputSuccess2"><div class="invalid-feedback">这不起作用.</div>

<div class=""><按钮类型=提交"class =btn btn-secondary">文本</button>

</表单>

https://codeply.com/go/45rU7UOhFo

表单验证示例演示 - Bootstrap 4.0.0

<小时>

如文档中所述,如果您打算使用服务器端验证,您只需设置 is-valid>is-invalid 表单控件上的类...

<div class="form-group"><label class="form-control-label";for=inputSuccess1">输入成功</label><输入类型=文本"class="表单控件有效";id="inputSuccess1"><div class="valid-feedback">成功!你已经做到了.</div>

</表单>

I'm trying to implement validation by Bootstrap and I've pasted the following sample on my page:

<div class="form-group has-success">
  <label class="form-control-label" for="inputSuccess1">Input with success</label>
  <input type="text" class="form-control form-control-success" id="inputSuccess1">
  <div class="form-control-feedback">Success! You've done it.</div>
  <small class="form-text text-muted">Example help text that remains unchanged.</small>
</div>

I can see that the appearance of the input control has changed (it's a bit rounded and much more aesthetic now) but it still doesn't show the green border as can be seen on the page linked to. The Bootstrap I'm linking to is pointed out as follows.

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

I have tried to google for this issue but to no avail. I have a fiddle illustrating the issue.

What can I do about it? What am I missing?

解决方案

Bootstrap 5 (Update 2021)

Since jQuery is no longer required for Bootstrap 5, it's easy to do client-side validation with vanilla JavaScript. The docs include a generic code example that should work on all forms with needs-validation..

(function () {
    'use strict'

    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.querySelectorAll('.needs-validation')

    // Loop over them and prevent submission
    Array.prototype.slice.call(forms)
        .forEach(function (form) {
        form.addEventListener('submit', function (event) {
            if (!form.checkValidity()) {
            event.preventDefault()
            event.stopPropagation()
            }

            form.classList.add('was-validated')
        }, false)
        })
})()

Bootstrap 5 Form Validation Demo

Bootstrap 4 (Original Answer)

Validation has changed as of the Bootstrap 4 beta release.

The valid state selectors use the was-validated class which would be added dynamically after validating the form via client-side JavaScript. For example...

<form class="container was-validated" novalidate="">
    <div class="form-group">
        <label class="form-control-label" for="inputSuccess1">Input with success</label>
        <input type="text" class="form-control" name="i1" id="inputSuccess1">
        <div class="valid-feedback">Success! You've done it.</div>
    </div>
    <div class="form-group">
        <label class="form-control-label" for="inputSuccess2">Input with danger</label>
        <input type="text" class="form-control" name="i2" required="" id="inputSuccess2">
        <div class="invalid-feedback">That didn't work.</div>
    </div>
    <div class="">
        <button type="submit" class="btn btn-secondary">Text</button>
    </div>
</form>

https://codeply.com/go/45rU7UOhFo

Form Validation Example Demo - Bootstrap 4.0.0


As explained in the docs, if you intend to use server-side validation you can simply set the is-valid or is-invalid classes on the form-controls...

<form class="container">
    <div class="form-group">
        <label class="form-control-label" for="inputSuccess1">Input with success</label>
        <input type="text" class="form-control is-valid" id="inputSuccess1">
        <div class="valid-feedback">Success! You've done it.</div>
    </div>
</form>

这篇关于无法在 Bootstrap 中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆