2012-02-23 15 views
1

的的jsfiddle:http://jsfiddle.net/hbrls/Snn87/12/jQuery驗證 「需要(依賴性的回調)」 的消息不工作

我有一個驗證代碼字段:<input type="text" id="verfy_code" name="verfy_code" class="required" />

和驗證的js是:

$("#aspnetForm").validate({ 
      rules: { 
       verfy_code: { 
        required: function(element) { return check_verfy_code(); } 
       } 
      }, 
      messages: { 
       verfy_code: { required: "verify code not correct" } 
      } 
     }); 
    function check_verfy_code() { 
     var flag = false; 
     //some ajax to check if verfy_code equalto session["verfy_code"] 
     //I've not implemented this, so this function will always return false 
     return flag; 
    } 

消息從不顯示。

+0

可你[的jsfiddle(HTTP此設置:// WWW .jsfiddle.net),這樣我們可以更容易地嘗試一下嗎? – Vikas 2012-02-23 03:57:09

+0

@Vikas好主意。但我不熟悉jsFiddle。現在沒有用。 – hbrls 2012-02-23 04:11:02

回答

1

檢查文檔爲required(dependency-callback)選項:因爲它是唯一的參數

的功能與元素執行:如果 返回true,需要的元素。

你總是返回false,因此該元素是從未需要。

如果您從函數返回true你所依賴的回調將工作像您期望:

function check_verfy_code() { 
    var flag = true; 
    //some ajax to check if verfy_code equalto session["verfy_code"] 
    //I've not implemented this, so this function will always return false 
    return flag; 
} 

例子:http://jsfiddle.net/jZjFq/

+0

謝謝。這就是原因。 – hbrls 2012-02-23 05:40:45