1

我在表单中使用 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>
4

2 回答 2

0

它可能是:

fields: function(){

    if(your evaluator here){

        return name: {etc}

    }
    else{

        return BusinessName: {etc}

    }

}
于 2014-11-05T21:57:30.060 回答
0

这是我的解决方案http://jsfiddle.net/silviagreen/4jcdnn14/

.on('change', '[name="isPerson"]', function () {

    var bootstrapValidator = $('#form_signup').data('bootstrapValidator'),
        isPerson = ($(this).val() == 'true');

    if (isPerson) {
        $('.persona').find('.form-control').removeAttr('disabled');
        $('.azienda').find('.form-control').attr('disabled', 'disabled');


        bootstrapValidator.enableFieldValidators('luogo_di_nascita', true)
            .enableFieldValidators('data_di_nascita', true)
            .enableFieldValidators('cf', true)
            .enableFieldValidators('sede', false)
            .enableFieldValidators('ragione_sociale', false);

        bootstrapValidator.revalidateField('luogo_di_nascita')
            .revalidateField('data_di_nascita')
            .revalidateField('cf');

    } else {
        $('.azienda').find('.form-control').removeAttr('disabled');
        $('.persona').find('.form-control').attr('disabled', 'disabled');

        bootstrapValidator.enableFieldValidators('luogo_di_nascita', false)
            .enableFieldValidators('data_di_nascita', false)
            .enableFieldValidators('cf', false)
            .enableFieldValidators('sede', true)
            .enableFieldValidators('ragione_sociale', true);

        bootstrapValidator.revalidateField('sede')
            .revalidateField('ragione_sociale');

    }






    })

根据“isPerson”的值,只验证部分字段。

于 2015-11-30T17:29:13.913 回答