2012-03-20 70 views
0

我正在創建一個註冊表單,並且我想檢查用戶是否在輸入密碼的同時給他發送相應的消息。到目前爲止一切正常,除了這些東西:當兩個字段都爲空時,檢查密碼是否匹配會返回「密碼匹配」

  • 當用戶從確認密碼字段刪除一切仍 給他發個消息「密碼不匹配」,而我想給他 沒有消息或消息說「請確認密碼」。
  • 當 用戶從兩個字段中刪除所有內容時,它會給他一條消息 「密碼匹配」,而它根本不應該給他任何消息。

這裏是我的代碼:

$(function() { 
    $("#txtNewPassword").keyup(checkPasswordMatch); 
    $("#txtConfirmPassword").keyup(checkPasswordMatch); 
}); 


function checkPasswordMatch() { 
    $("#divCheckPasswordMatch").html(""); 
    var password = $("#txtNewPassword").val(); 
    var confirmPassword = $("#txtConfirmPassword").val(); 

    if (password == "" && confirmPassword == ""){ 
     $("#divCheckPasswordMatch").html(""); 
     $("#divIsPasswordExist").html(""); 
    } 
    else if (password != "" && confirmPassword == "") { 
     $("#divCheckPasswordMatch").html(""); 
    } 

    else if (password == "" && confirmPassword != "") 
     $("#divIsPasswordExist").html("Password cannot be empty!"); 
    else 
     $("#divIsPasswordExist").html(""); 


    if (password != confirmPassword) 
    { 
     $("#divCheckPasswordMatch").removeClass("registrationFormConfirm"); 
     $("#divCheckPasswordMatch").addClass("registrationFormAlert"); 
     $("#divCheckPasswordMatch").html("Passwords do not match!"); 
    } 
    else 
    { 
     $("#divCheckPasswordMatch").removeClass("registrationFormAlert"); 
     $("#divCheckPasswordMatch").addClass("registrationFormConfirm"); 
     $("#divCheckPasswordMatch").html("Passwords match."); 
    } 
} 

任何想法,請? 謝謝!

+0

我想'return'幫助你很多關於給予適當的消息,並從特定的地方,而不是執行整個代碼返回。 – 2012-03-20 11:34:16

+0

與問題沒有直接關係,但您可能有興趣檢測HTML5 ['onInput'](http://mathiasbynens.be/notes/oninput)事件而不是'onKeyUp'。 – 2012-03-20 12:05:11

回答

1
...... 
     if (password == "" && confirmPassword == ""){ 
      $("#divCheckPasswordMatch").html(""); 
      $("#divIsPasswordExist").html("Password and Confirm Password dosen't exists"); 
      return; 
     } 
     else if (password != "" && confirmPassword == "") { 
      $("#divCheckPasswordMatch").html("Confirm Password dosent't exists"); 
      return; 
     } 
     else if (password == "" && confirmPassword != ""){ 
      $("#divIsPasswordExist").html("Password cannot be empty!"); 
      return; 
     } 
     else{ 
      $("#divIsPasswordExist").html(""); 
     } 
     if (password != confirmPassword) 
     { 
      $("#divCheckPasswordMatch").removeClass("registrationFormConfirm"); 
      $("#divCheckPasswordMatch").addClass("registrationFormAlert"); 
      $("#divCheckPasswordMatch").html("Passwords do not match!"); 
     } 
     else 
     { 
      $("#divCheckPasswordMatch").removeClass("registrationFormAlert"); 
      $("#divCheckPasswordMatch").addClass("registrationFormConfirm"); 
      $("#divCheckPasswordMatch").html("Passwords match."); 
     } 
......... 
+0

謝謝!工作很棒! – Igal 2012-03-20 13:04:37

2

這是因爲if (password != confirmPassword)是錯誤的,因爲無論如何都是空的,當你設置var password = $("#txtNewPassword").val();var confirmPassword = $("#txtConfirmPassword").val();這兩者都相互相等。在檢查值是否爲空時,應該添加一個返回值,這樣當用戶刪除文本時,它不會檢查第一個檢查語句下面的任何內容。

//首先檢查

if (password == "" && confirmPassword == ""){ 
return false; 
} 

...代碼

1
if (password == "" && confirmPassword == ""){ 
    return false; 
} 
    .... 
    .... 

if(confirmPassword != "") 
    $("#txtConfirmPassword").html("Please confirm password"); 

     if (password != confirmPassword) 
     { 
      $("#divCheckPasswordMatch").removeClass("registrationFormConfirm"); 
      $("#divCheckPasswordMatch").addClass("registrationFormAlert"); 
      $("#divCheckPasswordMatch").html("Passwords do not match!"); 
     } 
     else 
     { 
      $("#divCheckPasswordMatch").removeClass("registrationFormAlert"); 
      $("#divCheckPasswordMatch").addClass("registrationFormConfirm"); 
      $("#divCheckPasswordMatch").html("Passwords match."); 
     } 

... 
... 
1

有一個更優雅的方式來做到這一點休息。改變你的條件,這

if(password.length > 0 && confirmPassword.length >0) { 
    if(password == confirmPassword) { 
     //confirmed 
    } else { 
     // not confirm 
    } 

} else { 
    //not entered 
}