添加自定义函数到formValidation.js配置

时间:2015-06-20 12:10:10

标签: javascript jquery formvalidation-plugin

我有以下配置,在按下提交按钮时验证表单:

 $('#myForm').formValidation({
    framework: 'bootstrap',
    excluded: [':disabled'],
    icon: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        title: {
            validators: {
                notEmpty: {
                    message: 'Title is required (max 128 characters)'
                }
            }
        },
        rating: {
            validators: {
                notEmpty: {
                    message: // Call function here? check for 0
                }
            }
        }            
    }
});

评级字段是一个滑块,用户可以选择0到5之间的值

enter image description here

默认情况下它是0,所以当我按下提交时,它验证为有效,所以我需要创建一个自定义函数,如果它为0则检查其值,否则返回无效。

html如下:

 <input id="Rating" name="rating" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="5" data-slider-step="1" data-slider-value="" /> <span id="rate" style="margin-left:5%">0 / 5</span>

HTML在渲染时的外观,并引用了bootstrap-slider.js

<input id="Rating" type="text" data-slider-value="" data-slider-step="1" data-slider-max="5" data-slider-min="0" data-slider-id="ex1Slider" name="rating" data-fv-field="rating" style="display: none;" data="value: '0'" value="0">

1 个答案:

答案 0 :(得分:0)

为什么不试试greaterThan验证器:

rating: {
    validators: {
        greaterThan: {
            value: 0
        }
    }
}