2

I am using Bootstrap validator and the problem I am having is that I want to enable to submit button after all values are valid but unable to do so.

$(document).ready(function() {
    $('#ans_frm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: 'button[type="submit"]',
        fields: {
            ans: {
                group: '.col-md-8',
                validators: {

                    stringLength: {
                        min: 3,
                        max: 100,
                        message: 'The answer must be more than 2 and less than 100 characters long'
                    },
                    notEmpty: {
                        message: 'The answer must not be empty'
                    }
                }

            }
        }
    }).on('status.field.bv', function(e, data) {

                disableSubmitButtons(false);
            }
        });
});
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
Umerm
  • 145
  • 1
  • 2
  • 9

2 Answers2

3

You need to disable the submit button .on('error.field.bv') and enable it back on .on('status.field.bv').

And you should be using data.bv.disableSubmitButtons() method!

Can you try this?

$(document).ready(function() {
    $('#ans_frm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: 'button[type="submit"]',
        fields: {
            ans: {
                group: '.col-md-8',
                validators: {
                    stringLength: {
                        min: 3,
                        max: 100,
                        message: 'The answer must be more than 2 and less than 100 characters long'
                    },
                    notEmpty: {
                        message: 'The answer must not be empty'
                    }
                }
            }
        }
    }).on('error.field.bv', function(e, data) {
            data.bv.disableSubmitButtons(true); // disable submit buttons on errors
        }
    }).on('status.field.bv', function(e, data) {
            data.bv.disableSubmitButtons(false); // enable submit buttons on valid
        }
    });
});
dannytranlx
  • 175
  • 8
  • Its does send values to the next page but it does not validate.The button is enabled all the time even when the values are not right. – Umerm Nov 09 '14 at 18:26
2

Use this

$('#ans_frm').bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            live: 'enabled',
            trigger: null
        }).on('success.form.bv', function (e) {
            // Prevent submit form
            // e.preventDefault();
        })
      .on('error.form.bv', function () {

      });