简体   繁体   English

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

[英]Adding a custom function to formValidation.js configuration

I have the following configuration which validates a form when a submit button is pressed: 我具有以下配置,当按下提交按钮时,该配置可验证表单:

 $('#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
                }
            }
        }            
    }
});

The rating field is a slider, whereby the user can choose a value between 0 and 5 评分字段是一个滑块,用户可以在其中选择0到5之间的一个值

在此处输入图片说明

By default it is 0, so when I press submit it validates as valid, so I need to create a custom function which will check its value if it's 0 then return invalid otherwise true. 默认情况下它是0,所以当我按Submit时它会验证为有效,因此我需要创建一个自定义函数,该函数将检查其值是否为0,然后返回invalid,否则返回true。

The html is as follows: 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>

How the HTML looks when its render, and referenced bootstrap-slider.js 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">

Why not try the greaterThan validator: 为什么不尝试大于验证器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM