【问题标题】:How to reject use of certain words in a form?如何拒绝在表格中使用某些单词?
【发布时间】:2014-03-02 00:50:18
【问题描述】:

我是 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; 
        }
    }
}

【问题讨论】:

  • 您确定要在 JavaScript 中执行此操作吗?这是一种客户端语言,任何人都可以查看源代码..
  • 如果您的输入不等于第一个输入,它将转到您放置返回的其他位置。所以删除 else 块
  • @Mathijs Flietstra 是正确的——您至少应该在服务器端验证这一点,并添加 javascript 验证作为一个不错的选择。依靠 javascript 验证来解决这个问题可能是灾难性的!
  • 由于您是 JS 新手,这是一个众所周知的事实:无论您以何种方式编写 JavaScript,它总是在客户端编译和运行。这意味着无论您尝试采取何种预防措施,每个人都可以轻松阅读您编写的所有代码 - 包括该用户名数组。这是@MathijsFlietstra 对他的评论赞不绝口的众多原因之一。
  • 感谢您分享此内容。我这样做只是为了锻炼目的。再次感谢。

标签: javascript arrays validation input


【解决方案1】:

试试这个:

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很简单。

【讨论】:

    【解决方案2】:

    你需要添加return false;到带有警报的代码部分,而不是 else 语句。

    【讨论】:

      【解决方案3】:

      这应该为您指明正确的方向:

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

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

      我也希望你也验证这个服务器端,否则用户仍然可以使用你的“nono”用户名。

      【讨论】:

        【解决方案4】:

        这里的 cmets 应该可以帮助您了解为什么您的代码没有按预期工作:

        // 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.
        
            }
        }
        

        正如许多人所指出的,客户端验证应被视为一个可选的附加功能,而服务器端验证是必不可少的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-17
          • 1970-01-01
          • 1970-01-01
          • 2012-02-23
          • 1970-01-01
          相关资源
          最近更新 更多