2013-02-26 62 views
0

我正在構建一個註冊表單,並且在用戶名可用時啓用提交按鈕的JavaScript部分有問題,密碼足夠複雜,並且確認密碼與第一個密碼匹配。出於某種原因,passwordStrong始終爲false,並且passwordsMatch始終未定義。函數退出後,全局變量的值錯誤

userNameAvailable = false 
passwordStrong = false 
passwordsMatch = false 
submitButton = null; 

function checkAvailability(name, avalabilityDiv) 
{ 
    if(name.length == 0)//this happens when a name is entered but backspaced OR the shift key is pressed (and nothing else) 
    { 
     avalabilityDiv.innerHTML = "Please enter a name" 
     userNameAvailable = false 
    } 
    else 
    { 
     var xmlhttp, responseText 
     if(window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200) 
      { 
       responseText = xmlhttp.responseText; 
       avalabilityDiv.innerHTML = responseText//responseText is the property of XMLHttpRequest object that holds server's response 
       if(responseText == "available") 
        userNameAvailable = true 
       else 
        userNameAvailable = false 
      } 
     } 
     xmlhttp.open("GET", "process.php?pName="+name,true); 
     xmlhttp.send(); 
    } 
} 

/* 
Checks if password contains at least two characters from at least two categories: letter, number, other 
sets passwordStrong true if does, otherwise false 
*/ 
function passwordStrength(pass, strengthDiv) 
{ 
    if(pass.length < 8)//check passwords length 
    { 
     strengthDiv.innerHTML = "too short!" 
     //passwordStrong = false 
     return 
    } 
    var group = [0, 0, 0, 0] 
    for(i = 0, c = pass.charAt(0); i < pass.length; ++i, c = pass.charAt(i)) 
     group[typeOfChar(c)]++ 

    var result = 0 
    for(var i = 0; i < group.length; i++) 
     if(group[i] >= 2) 
      result++ 

    if(result >= 2) 
    { 
     var passwordStrong = true 
     strengthDiv.innerHTML = "Good." 
     alert("passwordStrong: "+passwordStrong) 
    } 
    else 
    { 
     //var passwordStrong = false 
     strengthDiv.innerHTML = "too simple!" 
    } 
} 

/*used by isPasswordStrong()*/ 
function typeOfChar(c) 
{ 
    var ascii = c.charCodeAt(0) 

    if(ascii >= 48 && ascii <= 57)//is a digit 
     return 0 
    else if(ascii >= 65 && ascii <= 90)//is upper case letter 
     return 1 
    else if(ascii >= 97 && ascii <= 122)//is lower case letter 
     return 2 
    else//is other character(e.g. grammar character) 
     return 3 
} 

/* 
Checks to see if confirmation password is same as first password 
sets passwordsMatch true if same, otherwise false 
*/ 
function matchPasswords(first, second, matchDiv) 
{ 
    matchDiv.innerHTML = "first: "+first.value+" second: "+second.value+"<br />"+userNameAvailable+" "+passwordStrong+" "+passwordsMatch+" done" 
    if(first.value == second.value) 
     var passwordsMatch = true 
    else 
     var passwordsMatch = false 
} 

/*This always shows that passwordStrong is false*/ 
function output() 
{ 
    alert("From output() passwordStrong: "+passwordStrong) 
} 

function toggleSubmit() 
{ 
    alert("here1 passwordStrong: "+passwordStrong) 
    if(submitButton == null)//may be cached 
     submitButton = document.getElementById("submit") 
    if(userNameAvailable && passwordStrong && passwordsMatch) 
     submitButton.disabled = false 
    else 
     submitButton.disabled = true 
} 

即使HTML「好」。在功能退出後,顯示爲passwordStrength()變量passwordStrong變爲false。爲什麼?

我沒有使用JQuery。

編輯:是的,它修復了它。你們怎麼知道的?我所有的故障排除技術都沒有提供這個資源。

回答

4

當您在函數內引用這些變量時,請刪除var關鍵字。

通過使用var,您使本地變量具有與相對全局變量相同的名稱,因此全局變量是「隱藏的」。

1

var在你所在的地方範圍內創建一個變量(在你的情況下通常在函數內部)。此過載更高範圍內的任何變量,包括全局變量。相反,在開始時對所有「全局」變量使用var,並且從不在其他任何範圍內再次使用var

1

您已經在passwordStrength函數的本地作用域中聲明瞭一個新的變量passwordStrong,所以passwordStrength函數永遠不會碰到全局變量「passwordStrong」。

if(result >= 2) 
{ 
    var passwordStrong = true // i mean this one... 
    strengthDiv.innerHTML = "Good." 
    alert("passwordStrong: "+passwordStrong) 
} 

應更換如下

if(result >= 2) 
{ 
    passwordStrong = true; 
    strengthDiv.innerHTML = "Good." 
    alert("passwordStrong: "+passwordStrong) 
}