JavaScript Bootstrap 验证

标签 javascript jquery twitter-bootstrap jqbootstrapvalidation

我在表单中使用 JavaScript 和 Bootstrap 进行验证。当我将所有这 4 个字段设置为必填字段时,一切都会正常进行。

但是,我需要一个非此即彼的情况。例如,如果填写了前两个字段“name”和“SIN”,则不需要“BusinessName”和“BusinessID”,反之亦然。这是当前代码:

<div class="form-group">
    <label class="col-lg-3 control-label">Name</label>
    <div class="col-lg-5">
        <input type="text" required class="form-control" name="name" />
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">SIN #</label>
    <div class="col-lg-5">
        <input type="text" class="form-control" name="SIN" />
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">Business Name</label>
    <div class="col-lg-5">
        <input type="text" class="form-control" name="BusinessName" />
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">Business #</label>
    <div class="col-lg-5">
        <input type="text" class="form-control" name="Businessid" />
    </div>
</div>  

<script type="text/javascript">
$(document).ready(function() {
    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',

        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: 'The Name is required and cannot be empty'
                    },

                    regexp: {
                        regexp: /^[a-zA-Z-\.\ ]+$/,
                        message: 'The Name can only consist of Alphabets, Space and Hyphens'
                    }
                }
            },


            SIN: {
                validators: {
                    notEmpty: {
                        message: 'The SIN is required and cannot be empty'
                    },

                    regexp: {
                        regexp: /^[0-9\ ]+$/,
                        message: 'The SIN can only consist of Numbers and Space'
                    }
                }
            },


            BusinessName: {
                validators: {
                    notEmpty: {
                        message: 'The Business name is required and cannot be empty'
                    }
                }

            },


            Businessid: {
                validators: {
                    notEmpty: {
                        message: 'The Business# is required and cannot be empty'
                    },

                    regexp: {
                        regexp: /^[0-9]+$/,
                        message: 'The Business# can only consist of Numbers'
                    }
                }
            },
des1: {

}


        }
    }) .on('success.form.bv', function(e) {

        });
});
</script>

最佳答案

可能是:

fields: function(){

    if(your evaluator here){

        return name: {etc}

    }
    else{

        return BusinessName: {etc}

    }

}

关于JavaScript Bootstrap 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767719/

相关文章:

javascript - 在多个文件中声明时,使函数在 Javascript IFFE 中公开

jquery - 苹果链接下拉菜单

javascript - Bootstrap 弹出链接不起作用

javascript - Bootstrap 按钮 onclick 不起作用

html - Bootstrap 中心表单

javascript - 如何使用 Ext Js 4.2 在基本网格中添加本地过滤选项?

php - 如何通过更改选择来更改输入(文本框)中的值

javascript - HighCharts:创建股票图表时出现奇怪的错误

javascript - 从数据库 mysql php 删除引导警报

javascript - 使用 json 的 slider