Jquery Validate custom error message location

后端 未结 7 1769
一个人的身影
一个人的身影 2021-01-30 16:35

This looks very simply, but I can\'t figure it out. I\'m using the jquery validate plugin. I\'m trying to validate and

7条回答
  •  悲&欢浪女
    2021-01-30 17:23

    What you should use is the errorLabelContainer

    jQuery(function($) {
      var validator = $('#form').validate({
        rules: {
          first: {
            required: true
          },
          second: {
            required: true
          }
        },
        messages: {},
        errorElement : 'div',
        errorLabelContainer: '.errorTxt'
      });
    });
    .errorTxt{
      border: 1px solid red;
      min-height: 20px;
    }
    
    
    
    
    


    If you want to retain your structure then

    jQuery(function($) {
      var validator = $('#form').validate({
        rules: {
          first: {
            required: true
          },
          second: {
            required: true
          }
        },
        messages: {},
        errorPlacement: function(error, element) {
          var placement = $(element).data('error');
          if (placement) {
            $(placement).append(error)
          } else {
            error.insertAfter(element);
          }
        }
      });
    });
    #errNm1 {
      border: 1px solid red;
    }
    #errNm2 {
      border: 1px solid green;
    }
    
    
    
    
    

提交回复
热议问题