2011-09-21 15 views
0

我有一個自定義驗證方法,用於檢查重複的用戶名。 json會爲notDuplicateUsername正確返回,但驗證始終顯示爲無效。自定義jquery.validate方法始終顯示無效

$('#register-form').validate({ 

    //see source of http://jquery.bassistance.de/validate/demo/ for example 

    rules: { 
     schoolname: { 
      required: true 
     }, 
     username: { 
      required: true, 
      notDuplicateUsername: true 
     }, 
     password: { 
      required: true 
     }, 
     email: { 
      required: true, 
      email: true 
     } 

    }, 
    messages: { 
     schoolname: 'Please tell us where you want to use Word Strip.', 
     username: { 
      required: 'Please choose a username.', 
      notDuplicateUsername: 'Sorry, that username is already being used.' 
     }, 
     password: 'Please choose a password.', 
     email: 'Please can we have your email address.' 

    } 

}); 

jQuery.validator.addMethod(
    'notDuplicateUsername', 
    function(value, element, params){ 

     var toCheck = new Object(); 
     toCheck['username'] = $('#username').val(); 

     var data_string = $.toJSON(toCheck);//this is a method of the jquery.json plug in 

     $.post('check_duplicate_username.php', {username_data: data_string}, function(result){ 
      var noDuplicate = true; 

      var returned_data = $.evalJSON(result);//this is a method of the jquery.json plug in 

      if (returned_data.status == 'duplicate'){ 
       noDuplicate = false; 
      } 

      console.log('value of noDuplicate: '+noDuplicate); 
      return noDuplicate; 
     }); 

    } 
); 

任何線索有人嗎?

回答

0

也許你可能已經整理出來了,但以防萬一。我遇到類似的問題,並發現我正在比較錯誤的類型。可能是您的服務器將狀態作爲布爾值或其他數據類型發送,並且與字符串的比較失敗並始終返回false。

相關問題