2011-10-21 46 views
0

我很抱歉,我要粘貼相當雜亂的代碼,但幫助我,如果你可以 其實我正在實現一個密碼更改功能,其中更改密碼按鈕的onclientclick事件我打電話給一個JavaScript函數對此我確認三件事在jquery中的密碼驗證

  1. 當前密碼是否與舊密碼匹配(通過Ajax調用)
  2. 和新密碼和確認密碼字段包含相同的數據或不。
  3. ,當然任何字段是否爲空或不

第二和第三都工作正常,但在第一種情況下,即使密碼didnt使用舊密碼相匹配,而其他條件都滿足它正在改變密碼

的JavaScript函數就是

function check() { 
var isValid = true; 
    // Validate User Information. 
    if ($("input[id$='txtBoxPasswrd']").val().length >= 0 && $("input[id$='txtBoxPasswrd']").val().trim() != "") { 
     $.ajax({ 
      type: "POST", 
      url: "UserProfile.aspx/CheckCurrentUserPassword", 
      data: "{'userPasswrd':'" + $("input[id$='txtBoxPasswrd']").val() + "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       if (msg.d != null) { 
        var result = eval(msg.d); 
        if (result.toString() == "true") { 
         $("input[id$='txtBoxPasswrd']").next() 
         .removeClass() 
         .addClass('success') 
         .text("Ok"); 
        } 
        else { 
         $("input[id$='txtBoxPasswrd']").next() 
         .removeClass() 
         .addClass('failure') 
         .fadeIn('slow') 
         .text("Password doesn't match with your old password"); 

         isValid = false; 
        } 
       } 
      } 
     }); 
    } 
    else { 
     $("input[id$='txtBoxPasswrd']").next() 
         .removeClass() 
         .addClass('failure') 
         .fadeIn('slow') 
         .text("Can't be blank."); 
     isValid = false; 
    } 
    if ($("input[id$='txtNewPassword']").val().length > 0 && $("input[id$='txtNewPassword']").val().trim() != " ") { 
     if ($("input[id$='txtConfirm']").val().length > 0 && $("input[id$='txtNewPassword']").val() != $("input[id$='txtConfirm']").val()) { 
      $('#txtConfirm').next() 
             .removeClass() 
             .addClass('failure') 
             .fadeIn('slow') 
             .text("Password doesn't match."); 
      isValid = false; 
     } 
     else { 
      $("input[id$='txtNewPassword']").next() 
           .removeClass() 
           .addClass('success') 
           .fadeIn('slow') 
           .text("Ok"); 
     } 
    } 
    else { 
     $("input[id$='txtNewPassword']").next() 
          .removeClass() 
          .addClass('failure') 
          .fadeIn('slow') 
          .text("Can't be blank."); 
     isValid = false; 
    } 
    if ($("input[id$='txtConfirm']").val().length == 0 || $("input[id$='txtConfirm']").val().trim() == "") { 
     $("input[id$='txtConfirm']").next() 
          .removeClass() 
          .addClass('failure') 
          .fadeIn('slow') 
          .text("Can't be blank."); 
     isValid = false; 
    } 
    return isValid; 
} 

寫在Ajax調用的方法是OK,因爲它是返回正確的錯誤消息。 這是一些如何返回isValid爲true,刪除ajax調用方法,它工作正常的其他兩個驗證。 我在這裏做錯了什麼?

-Thanks MAC

+0

你爲什麼'eval(msg.d)'你想把它解析爲一個json? – Val

+0

實際上我只需要檢查ajax調用方法返回的值,所以我用這個 – Mac

+0

你可以在firebug上使用'console.log(msg)'來檢查它,或者使用chrome開發工具,根據這個值很好地顯示你的 – Val

回答

0

修改Ajax調用,使其工作

var html = $.ajax({ 
      type: "POST", 
      url: "UserProfile.aspx/CheckCurrentUserPassword", 
      data: "{'userPasswrd':'" + $("input[id$='txtBoxPasswrd']").val() + "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      async:false 
     }).responseText; 

     if (html.toString().indexOf("false") >= 0) { 
      isValid = false; 
     } 
     else { 
      $("input[id$='txtBoxPasswrd']").next() 
         .removeClass() 
         .addClass('success') 
         .fadeIn('slow') 
         .text("Ok"); 
     } 
    } 
1

實現與漂亮的錯誤提示驗證的最簡單的方法的jQuery插件EVAL減少的過度頭服務器交互。

看看下面的鏈接。

http://code.google.com/p/jquery-jval/

jQuery and jVal Validation Plug-in. Adding an attribute to an element

http://code.google.com/p/jquery-jval/source/browse/trunk/jVal.html

,如果你想使用JavaScript和jQuery纔去的Javascript正則表達式。看看javascript正則表達式並實現驗證輸入的方法。

如果你想遵循相同的使用成功:和錯誤:.ajax()的屬性,並獲得您返回的信息成功與msg.d,您發送回ajax請求。