1

我的表单中有 3 个字段。我想用一个错误类验证 Name、Age 和另一个错误类的 Total_score。这是一个包含单个 id 的单一表单。我怎样才能做到这一点。

$('#form1').validate({
   rules: {
     'Name' : {
       required   : true
     },
     'Age'   : {
       required: true

     },
     'Total_score' : {
        required: true
     }
   },
   messages: {
     'Name' : {
       required: 'From date is required!'
     },
     'Age'   : {
       required: 'To date is required!'
     },
     'Total_score' : {
       required: 'Score is required!'
     }
   },
   errorClass: '?????',
   errorPlacement: function(err, element) {
   err.insertBefore(element);
   },
   submitHandler: function(form) {
   form.submit();
   }
 });
4

1 回答 1

2

尝试类似的东西

<input name="Name" data-errorclass="error-name"/>

然后

$('#form1').validate({
    rules: {
        'Name' : {
            required   : true
        },
        'Age'   : {
            required: true

        },
        'Total_score' : {
            required: true
        }
    },
    messages: {
        'Name' : {
            required: 'From date is required!'
        },
        'Age'   : {
            required: 'To date is required!'
        },
        'Total_score' : {
            required: 'From date is required!'
        }
    },
    errorPlacement: function(err, element) {
        err.addClass(element.data('errorclass'))
        err.insertBefore(element);
    },
    submitHandler: function(form) {
        form.submit();
    }
});

演示:小提琴

于 2013-10-14T07:44:49.673 回答