Saravanan Saravanan - 3 months ago 51
jQuery Question

Dynamic Validation using jqBootstrapValidation

Suppose I have a button (type is not Submit) and an event attached to a button like below

$("#idProceedBtn").on("click", function (e) {.....}


I will need to perform the action in the event only after validation passes. I can achieve this by else condition of the below statement

if($("input,select,textarea").not("[type=submit]").jqBootstrapValidation("hasErrors")){.....}else{.....}


Doing this is working, but it is not highlighting which fields are failing validation and why, like what happens when a button with type submit clicked.

Answer

I've found a solution by modifying of "hasErrors" method in jqBootstrapValidation.js - I've added the trigger to "submit.validation":

hasErrors: function() {

    var errorMessages = [];

    this.each(function (i, el) {
      errorMessages = errorMessages.concat(
        $(el).triggerHandler("getValidators.validation") ? $(el).triggerHandler("validation.validation", {submitting: true}) : []
      );
      $(el).trigger("submit.validation");
    });

    return (errorMessages.length > 0);
},

Now it is highlighting errors in form as expected.