如何使用Bootstrap验证器对Bootstrap v4进行验证?

时间:2018-02-06 03:25:33

标签: html css twitter-bootstrap-4 bootstrapvalidator

我刚刚从v3迁移到Bootstrap v4。并且Bootstrap Validator部分正常工作。 JavaScript部分仍然有效。然而,造型已经消失。

这是我使用的bootstrap验证器:https://github.com/1000hz/bootstrap-validator

以下是它的外观: enter image description here

看起来像这样: enter image description here

我手动添加了css。因此错误消息有效。

Bootstrap验证器已经添加了.has-danger类。 如何在发生错误时修复标签突出显示和输入框高亮显示?

我已根据BS4转换了所有课程。这是我的代码:



$('#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>
&#13;
&#13;
&#13;

提前致谢!

1 个答案:

答案 0 :(得分:2)

首先,您的脚本文件不合规...... jQuery应该在最顶层......并且您已经使用了bootstrap-validator两次......不需要......

回答你的问题是你必须使用has-danger课来设定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>