首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带引导选择的FormValidation.io

带引导选择的FormValidation.io
EN

Stack Overflow用户
提问于 2020-07-23 05:22:34
回答 1查看 265关注 0票数 0

我使用的是Boost strap 4和bootstrap select jQuery插件。

我正在尝试将FormValidation.io与我的表单集成。我已经让一切正常的输入字段工作,但不知道如何将它与选择字段集成。

我希望它是一个必填字段,并显示勾选图标一旦作出选择。

我的FormValidation.io代码:

代码语言:javascript
复制
document.addEventListener('DOMContentLoaded', function(e) {
const mydropzone = document.getElementById('mydropzone');

const RoleIdField = jQuery(mydropzone.querySelector('[name="roleId"]'));    

const fv = FormValidation.formValidation(mydropzone, {    
       
    
        fields: {
            first_name: {
                validators: {
                    notEmpty: {
                        message: 'First Name is required'
                    },                        
                    regexp: {
                        regexp: /^[a-zA-Z]+$/,
                        message: 'First Name can only consist of alphabetical characters'
                    }
                }
            },
            last_name: {
                validators: {
                    notEmpty: {
                        message: 'Last Name is required'
                    },                        
                    regexp: {
                        regexp: /^[a-zA-Z]+$/,
                        message: 'First Name can only consist of alphabetical characters'
                    }
                }
            },
            roleId: {
                validators: {
                    notEmpty: {
                        message: 'Please select a Role'
                    },  
                }
            },
           
        },
        plugins: {
            trigger: new FormValidation.plugins.Trigger(),
            bootstrap: new FormValidation.plugins.Bootstrap(),
            submitButton: new FormValidation.plugins.SubmitButton(),
            icon: new FormValidation.plugins.Icon({
                valid: 'fa fa-check',
                invalid: 'fa fa-times',
                validating: 'fa fa-refresh'
            }),
        }
    }
);
});


$('#roleId').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
// Revalidate the color field when an option is chosen
fv.revalidateField('roelId');
});

我的表单ID是'mydropzone‘,我的选择名称和id是'roleId’

感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-25 01:14:44

感谢回复我电子邮件的开发人员,任何其他需要知道这是如何做到的人:

将此代码添加到css文件中:

代码语言:javascript
复制
.bootstrap-select i.fv-plugins-icon {
right: -38px !important;

然后像这样配置你的js:

代码语言:javascript
复制
<script>
document.addEventListener('DOMContentLoaded', function(e) {
$('#gender').selectpicker();

const demoForm = document.getElementById('demoForm');
FormValidation.formValidation(demoForm, {
    fields: {
        gender: {
            validators: {
                notEmpty: {
                    message: 'The gender is required'
                }
            }
        },
    },
    plugins: {
        trigger: new FormValidation.plugins.Trigger(),
        submitButton: new FormValidation.plugins.SubmitButton(),
        bootstrap: new FormValidation.plugins.Bootstrap(),
        icon: new FormValidation.plugins.Icon({
            valid: 'fa fa-check',
            invalid: 'fa fa-times',
            validating: 'fa fa-refresh'
          }),
       }
    });
});
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63043408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档