简体   繁体   中英

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

在此处输入图片说明

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.

The html is as follows:

 <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

<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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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