我可以结合使用jquery的两个组合框的验证错误? [英] Can I combine the validation-error of two Combo box using jquery?

查看:112
本文介绍了我可以结合使用jquery的两个组合框的验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图验证3个组合框的日期,月份,年份。

I am trying to validate 3 combobox which are for date, month, year.

我使用jquery.js和jquery.validate.js。

I am using jquery.js and jquery.validate.js. It is showing errors for each combobox if it is not selected, but I want only one message instead of 3 errors.

推荐答案

如果没有选择,每个组合框显示错误,但我只需要一个消息,而不是3个错误。需要将所有3添加到,并设置

Two things are needed - add all 3 to a group, and setup the errorPlacement option to appropriately place the error message.

因此,如果您的表单如下所示:

So if your form looks like this:

<form>
  <select name="day" class="required">
     <option value="">Day</option>
     <option value="1">1</option>
     <!-- etc -->
  </select>
  <select name="month" class="required">
     <option value="">Month</option>
     <option value="1">Jan</option>
     <!-- etc -->
  </select>
  <select name="year" id="year" class="required">
     <option value="">Year</option>
     <option value="2013">2013</option>
     <!-- etc -->
  </select>
  <br>
  <input type="submit">
</form>

然后你需要一个jQuery Validate调用,如下所示:

Then you need a jQuery Validate call like this:

$('form').validate({
   groups: {
      myDate: 'day month year'
   },
   errorPlacement: function(error, element) {  
     if (element.attr("name") == "day" 
             || element.attr("name") == "month" 
             || element.attr("name") == "year" )
        error.insertAfter("#year");
     else
        error.insertAfter(element);
   }
});

如果你把它们放在一起,你会得到一个如下示例: http://jsfiddle.net/ryleyb/Yzdag/

And if you put that all together you get an example that looks like this: http://jsfiddle.net/ryleyb/Yzdag/

这篇关于我可以结合使用jquery的两个组合框的验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆