2012-12-07 33 views
0

在驗證的形式,它工作正常,但顯示的是最後一個元素的錯誤,首先

這是第一次給消息Description然後Code然後Name。 但我想要的驗證應該是首先爲Name然後Code然後Description

$(document).ready(function() { 
    $("#added").validate({ 
     rules: { 
      Name: { 
       required: true 
      }, 
      Code: { 
       required: true 
      }, 
      Description: { 
       required: true 
      }, 

     }, 

     messages: { 
      Name: { 
       required: "Enter Name" 
      }, 
      Code: { 
       required: "Enter code" 
      }, 

      Description: { 
       required: "Enter Description" 
      }, 
     }, 

     errorPlacement: function(error, element) { 
      $("#error-messages").html(error); // error container 
     } 
    });​ 
}); 

HTML:

<div id="error-messages"></div> 
<s:form action="added" id="added" method="post" > 
    <s:textfield name="Name" id="Name" label="Name" labelposition="top" /> 
    <s:textarea name="Code" id="Code" rows="10" cols="50" 
       label="Code" labelposition="top"/> 

    <s:textarea name="Description" id="Description" rows="5" cols="50" 
       label="Description" labelposition="top"/> 
<s:form> 

如何解決這個問題?

+0

試圖扭轉了驗證規則順序? –

+0

重複此問題:http://stackoverflow.com/questions/5679480/specifiy-the-order-of-the-validators-in-jquery-validate-plugin? –

+0

@SamuelCaillerie我沒有得到重複的問題,你可以請用上面的例子 – xrcwrn

回答

0

jQuery validation plugin以DOM的順序檢索元素(請參閱$.validator.prototype.elements函數,1.461)。

因此,爲了從去年到第一個元素,以驗證的解決方案是修補與此代碼這一功能在我們自己的腳本部分:

$.validator.prototype.elements = function() { 
    var validator = this, 
     rulesCache = {}; 

    // select all valid inputs inside the form (no submit or reset buttons) 
    return 
     $( // Added this in order to re-encapsulate in a jQuery object the reversed array (see below) 
     $(this.currentForm) 
     .find("input, select, textarea") 
     .not(":submit, :reset, :image, [disabled]") 
     .not(this.settings.ignore) 
     .filter(function() { 
      if (!this.name && validator.settings.debug && window.console) { 
       console.error("%o has no name assigned", this); 
      } 

      // select only the first element for each name, and only those with rules specified 
      if (this.name in rulesCache || !validator.objectLength($(this).rules())) { 
       return false; 
      } 

      rulesCache[this.name] = true; 
      return true; 
     }) 
     .get().reverse() // Added this in order to reverse the array order 
    ); 
} 
相關問題