How to use Twitter Bootstrap popovers for jQuery validation notifications?

后端 未结 16 1654
无人共我
无人共我 2020-11-30 16:49

I can make popovers appear using bootstrap easily enough, and I can also do validations using the standard jQuery validation plugin or the jQuery validation engine, but I ca

16条回答
  •  情深已故
    2020-11-30 17:28

    This is a hands-on example:

    $('form').validate({
        errorClass:'error',
        validClass:'success',
        errorElement:'span',
        highlight: function (element, errorClass, validClass) { 
            $(element).parents("div[class='clearfix']").addClass(errorClass).removeClass(validClass); 
        }, 
        unhighlight: function (element, errorClass, validClass) { 
            $(element).parents(".error").removeClass(errorClass).addClass(validClass); 
        }
    });
    

    enter image description here

    It doesn't really use bootstrap popovers, but it looks really nice and is easy to achieve.

    UPDATE

    So, to have popover validation you can use this code:

    $("form").validate({
      rules : {
        test : {
          minlength: 3 ,
          required: true
        }
      },
      showErrors: function(errorMap, errorList) {
        $.each(this.successList, function(index, value) {
          return $(value).popover("hide");
        });
        return $.each(errorList, function(index, value) {
          var _popover;
          _popover = $(value.element).popover({
            trigger: "manual",
            placement: "top",
            content: value.message,
            template: "

    " }); // Bootstrap 3.x : //_popover.data("bs.popover").options.content = value.message; // Bootstrap 2.x : _popover.data("popover").options.content = value.message; return $(value.element).popover("show"); }); } });

    You get something like this:

    enter image description here

    Check out the jsFiddle.

提交回复
热议问题