2014-02-05 40 views
0

我是Javascript的新用戶。 我需要輸入一個用戶名,然後驗證它。 輸入的用戶名不能是此數組中的元素之一:["admin", "administrator", "demo", "user"]等。數組可能更長。如何拒絕表單中某些單詞的使用?

我做到了這一點,但它只適用於數組中的第一個元素。

function arrayValidation() { 
    nonoUser = ["username", "admin", "administrator", "demo"]; 
    valueOfInput = document.getElementById("user").value; // "user" here is id of input 
    for (var i = 0; i < nonoUser.length; i++) { 
     if (valueOfInput == nonoUser[i]) { 
      alert("you cant use this username"); 
     }else { 
      return; 
     } 
    } 
} 
+3

您確定要在JavaScript中執行此操作嗎?這是一種客戶端語言,任何人都可以查看源代碼。 –

+0

如果您的輸入不等於第一個輸入,它將返回其他位置。所以刪除其他塊 –

+0

@Mathijs Flietstra是正確的 - 你應該至少在服務器端驗證這一點,並添加JavaScript驗證作爲一個很好的。依靠javascript驗證這可能是災難性的! –

回答

2

試試這個:

function arrayValidation() { 
    nonoUser = ["username", "admin", "administrator", "demo"]; 
    valueOfInput = document.getElementById("user").value; // "user" here is id of input 
    var isValid = true; 
    for (var i = 0; i < nonoUser.length && isValid; i++) { 
     if (valueOfInput == nonoUser[i]) { 
      isValid = false; 
     } 
    } 
    if (isValid) { 
     // Username is valid 
    }else{ 
     // Username is invalid 
    } 
} 

但是,你應該是在發送到服務器從不信任數據(驗證服務器端以及)
是微不足道的改變JS作爲用戶。

+0

這也適用。 –

+0

完美運作。謝謝 – user3222552

0

你需要添加返回false;到警報的代碼部分,而不是else語句。

1

這應該指向你在正確的方向:

document.getElementById("user").onkeyup = function(){ 
    var nonoUser = ["username", "admin", "administrator", "demo"]; 
    nonoUser.indexOf(this.value) === -1 ? false : this.value = ''; 
} 

演示:http://jsfiddle.net/Le2xC/

我也希望你驗證此服務器端爲好,否則用戶仍然可以使用您的「nono」用戶名。

0

這裏的意見可以幫助你明白爲什麼你希望你的代碼是不工作:

// Your original code: 

function arrayValidation() { 
    nonoUser = ["username", "admin", "administrator", "demo"]; 
    valueOfInput = document.getElementById("user").value; // "user" here is id of input 
    for (var i = 0; i < nonoUser.length; i++) { 
     if (valueOfInput == nonoUser[i]) { 
      alert("you cant use this username"); 
     } else { 

      // Using return here is the problem. 
      // This else block will run if a valid username was entered. 
      // The return statement stops execution of the function, thus ending the loop. 
      // So if the loop passes the test the first time, this return 
      // statement will stop the loop and function from completing 
      // the checks against the other keywords. 

      return; 
     } 
    } 
} 

這是你修改後的代碼爲你所期望的工作:

function arrayValidation() { 
    nonoUser = ["username", "admin", "administrator", "demo"]; 
    valueOfInput = document.getElementById("user").value; // "user" here is id of input 
    for (var i = 0; i < nonoUser.length; i++) { 
     if (valueOfInput == nonoUser[i]) { 
      alert("you cant use this username"); 

      // We can end the function check return here. 
      // There's no point checking the others, as we've found an invalid one. 

      return false; // Return a meaningful result 
     } 

     // No need for an else statement either. 

    } 
} 

由於許多已經指出,客戶端驗證應該被視爲可選的額外服務器端驗證。

相關問題