BootstrapValidator条件验证

BootstrapValidator conditional validation

本文关键字:验证 条件 BootstrapValidator      更新时间:2023-09-26

我正在使用BootstrapValidator,我有一些文本字段和一个带有2个选项的单选按钮,我需要验证文本字段,只有当单选按钮选项2被选中时,我怎么才能获得它?

格式是:

<form action="/it/Account/SignUp" class="form-horizontal" id="signup_form" method="post" role="form">
<label>Name</label>
<input type="text" name="name" class="form-control" id="name">
<label>Company</label>
<input type="text" name="company" class="form-control" id="company">
<input type="radio" name="usertype" value="0"><label>Private</label>
<input type="radio" name="usertype" value="1"><label>Company</label>
</form>

这是BootstrapValidator的JavaScript:

$('#signup_form').bootstrapValidator({
    message: 'This value is not valid',
    live: 'disabled',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        name: {
            validators: {
                notEmpty: {
                    message: 'required'
                },
            }                
        },
        company: {
            validators: {
                notEmpty: {
                    message: 'required'
                },
            }                
        },
    }
}

我只需要在'usertype'的单选值等于1时验证"company"字段

您应该查看callback验证选项

https://formvalidation.io/guide/validators/callback

您可以创建一个自定义函数进行验证,返回一个true或false的布尔值,然后您应该根据单选按钮是否被选中对fieldValue进行检查