0

I have using the bootstrap-validate for bootstrap 4 located here:

https://bootstrap-validate.js.org/installation.html

I am trying to get it to validate a select input which I have like this:

    <div class="form-group">
        <label for="numberOfBedrooms">Number of Bedrooms<span>*</span></label>
        <select class="form-control" name="numberOfBedrooms" id="numberOfBedrooms" required>
            <option value="">Please select</option>
            <option value="1">1 Bedroom</option>
            <option value="2">2 Bedrooms</option>
            <option value="3">3 Bedrooms</option>
            <option value="4">4 Bedrooms</option>
            <option value="5">5+ Bedrooms</option>
        </select>
    </div>

My script does this:

bootstrapValidate('#numberOfBedrooms', 'required:Please select one option');

But it doesn't appear to validate. There is no class assigned to the select input field or the form-group. Does anyone know why this isn't working?

4

1 回答 1

0

我会检查你是否正确地从图书馆开始。当我将您的代码引入JSFiddle并将此代码添加到其中时:

bootstrapValidate('#numberOfBedrooms', 'required:Please select one option', function (isValid) {
   if (isValid) {
       alert('Element is valid');
   } else {
       alert('Element is invalid');
   }
});

当我选择了有效/无效的输入时,它会正确地提醒我。不过需要注意的是,在查看控制台时会出现如下错误

Assertion failed: Input argument is not an HTMLInputElement

因此,它可能没有将选择视为正确的输入元素并正确读取该选择的长度。另外,您在 javascript 中的哪个位置调用了 validate 方法?我假设您有一个提交按钮,并且在该按钮的 onclick 中?

于 2019-07-23T19:58:40.057 回答