如何为jQuery.validator添加自定义消息?

时间:2015-09-18 09:39:57

标签: javascript jquery

_form.html.erb

    <div class="form-group grading_weighments_div">
    <%= f.fields_for :grading_weighments do |gw| %>
        <div class="grading_weighments_wrapper">
            <div class="form-group weights_div">
                <%= gw.label :weights ,class:"sr-only"%>
                    <%= gw.text_field :weights ,placeholder:"Weight", class:"form-control weights", autocomplete:"off" ,autofocus:true%>
            </div>
            <div class="form-group boxes_div">
                <%= gw.label :box_count ,class:"sr-only"%>
                    <%= gw.text_field :box_count, placeholder:"Boxes", class:"form-control boxes", autocomplete:"off" %>
            </div>
            <div class="form-group remove_icon_div">
                <%= gw.link_to_remove class:'btn btn-danger',  title:"Remove this weight", data: { toggle: "tooltip", placement:"bottom" }  do%>
                    <i class="fa fa-minus"></i>
                    <%end%>
            </div>
        </div>
        <%end%>
          <%= f.link_to_add :grading_weighments ,style:'display:none', class:'btn btn-primary add_grading_weighments' , title:"Add Gradings", data: { toggle: "tooltip", placement:"right" } do%>
                <i class="fa fa-plus"></i>
                <%end%>

</div>    

验证

jQuery.validator.addClassRules({
 weights:{
  required:true,
  number:true,
  min:1,
},
boxes:{
  required:true,
  digits:true,
  min:1
}

});

这里我想为上面的字段添加自定义消息。我在这里使用jQuery.validator.addClassRules。怎么可能。我在这里添加了message:{}。但不行。

1 个答案:

答案 0 :(得分:0)

基本上你在错误的地方做正确的事。

如果您查看the documentation,您会看到可以通过向要验证的HTML元素添加一些信息来设置自定义消息。

他们举例说明:

<input id="cemail" 
    data-msg-email="Please enter a valid email address" 
    data-msg-required="Please enter your email address" 
    data-rule-email="true" 
    data-rule-required="true" 
    name="email" 
    aria-required="true">

所有规则都引用表单元素名称而不是id。