2016-12-05 13 views
0

我有一個場景,當我有3個下拉菜單並需要對它們實施兩個驗證但是作爲一個組。當最後一個元素有效時,jQuery驗證邏輯失敗

1)3中的2個下拉框應該具有「否」以外的值。這與所需的驗證類似。不檢查空白將檢查「否」。

2)沒有2個下拉列表可以具有相同的值。

Fiddle

jQuery.validator.addMethod("require_from_group", function(value, element, options) { 
    var numberRequired = options[0]; 
    var selector = options[1]; 
    var fields = $(selector, element.form); 
    var filled_fields = fields.filter(function() { 
    // it's more clear to compare with empty string 
    return $(this).val() != "No"; 
    }); 
    var empty_fields = fields.not(filled_fields); 

    // we will mark only first empty field as invalid 
    if (filled_fields.length < numberRequired){ //&& empty_fields[0] == element) { 
    return false; 
    } 
    return true; 
// {0} below is the 0th item in the options field 
}, jQuery.format("Please fill out at least {0} of these fields.")); 

jQuery.validator.addMethod("notEqualToGroup", function(value, element, options) { 
var numberRequired = options[0]; 
var selector = options[1]; 
// get all the elements passed here with the same class 
var elems = $(element).parents('form').find(selector).not(element); 
// the value of the current element 
var valueToCompare = value; 
// count 
var matchesFound = 0; 
// loop each element and compare its value with the current value 
// and increase the count every time we find one 
jQuery.each(elems, function(){ 
    thisVal = $(this).val(); 
    if(thisVal == valueToCompare){ 
      matchesFound++; 
     } 
}); 

    if (matchesFound >= numberRequired){ //&& elems[0] != element) { 
    return false; 
    } 
    return true; 
}, jQuery.format("No two fields can have same value.")); 

其中一個場景中的失敗,即當選擇1和選擇2都不是「否」,但仍然有不顯示味精相同的值。

有人可以請建議缺少什麼。謝謝

+0

你爲什麼要使用一個7歲的版本jQuery驗證的?從那時起,許多bug在'require_from_group'中被修復。請升級到版本1.15.0並相應地重構您的代碼。 – Sparky

回答

1
  1. 將插件從近7年的版本1.7升級到the latest which is 1.15

  2. require_from_group的過時版本替換爲version 1.15中的最新版本。

  3. 更換jQuery.format("No two fields can have same value.")$.validator.format("No two...

你會發現,這個更新版本的作品更多的可預見的。


編輯

您還需要在你的notEqualToGroup規則完全重寫:

$.validator.addMethod("notEqualToGroup", function(value, element, options) { 

    var isvalid = true, 
     values = [], 
     temp = []; 

    // put all values into an array 
    $(options).each(function() { 
     values.push($(this).val()); 
    }); 

    // check array for duplicates 
    $.each(values, function(key, val) { 
     if ($.inArray(val, temp) === -1) { 
      temp.push(val); 
     } else { 
      isvalid = false; // duplicate found 
     } 
    }); 

    return isvalid; 
}, $.validator.format("No two fields can have same value.")); 

從本質上講,把所有三個值到一個數組,重複檢查。

.... 
select1: { 
    require_from_group: [2, ".at_least_one"], 
    notEqualToGroup: ".at_least_one" 
}, 
.... 

DEMOjsfiddle.net/nu4dcstv/

+0

感謝@sparky,實際上我實際上是使用1.11版本,但是有些時候我拿了一箇舊樣本並將其分叉。這裏是更新鏈接[fiddle](http://jsfiddle.net/uaqadLgd/4/)。 ** require_from_group **方法工作正常,但我無法設計** notEqualToGroup **。我想要的是當前元素值與其他組元素值進行驗證,如果找到匹配,則驗證失敗。 –

+0

[fiddle](http://jsfiddle.net/uaqadLgd/6/)我認爲我修復了停止腳本問題,但驗證立即運行,不等待按鈕被按下。此外,組的最後一個元素有效的情況下,不會生成任何msg stand still。希望明白我的觀點。 –

+0

@NikhilGupta,如果您只想在嘗試提交時觸發驗證,請設置'onfocusout:false'和'onclick:false'。似乎爲我工作:「NO,NO,YES」觸發錯誤:http://jsfiddle.net/uaqadLgd/7/ – Sparky