Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在网站上禁止用户名_Javascript_Html - Fatal编程技术网

Javascript 如何在网站上禁止用户名

Javascript 如何在网站上禁止用户名,javascript,html,Javascript,Html,如何确定文本输入是否为特定文本 我试过这个 <script> var b = document.getElementById('button') var u = document.getElementById('username') var p = document.getElementById('password') var bannedUsers = ["user1012"]; b.onclick =

如何确定文本输入是否为特定文本

我试过这个

<script>
    var b = document.getElementById('button')
    var u = document.getElementById('username')
    var p = document.getElementById('password')
    
    var bannedUsers = ["user1012"];
    
    b.onclick = function() {
      if(u.value.length <= 20 && p.value.length >= 6 && u.value.length >= 3 && !u.value === bannedUsers) {
        location.href = "";
    };
      
      if(u.value.length > 20) {
        return alert('Username needs to be below 20 characters.')
      } else if(u.value.length < 3) {
        return alert('Username needs to be above 2 characters')
      }
      
      if(p.value.length < 6) {
        return alert('Password needs to be over 6 characters.')
      }
      
      if(u.value === bannedUsers) {
        return alert('That username is banned.')
      }
    }
    </script>

var b=document.getElementById('按钮')
var u=document.getElementById('用户名')
var p=document.getElementById('密码')
var bannedUsers=[“user1012”];
b、 onclick=function(){
如果(u.value.length=6&&u.value.length>=3&&u.value===bannedUsers){
location.href=“”;
};
如果(u.value.length>20){
返回警报('用户名必须少于20个字符')
}否则,如果(u.value.length<3){
返回警报('用户名必须超过2个字符')
}
如果(p.value.length<6){
返回警报('密码需要超过6个字符')
}
如果(u.value==横幅){
返回警报('该用户名被禁止')
}
}

但它最终只是把我带到页面上,而不是说“此用户名被禁止”

您需要使用
includes
方法

bannedUsers.includes(u.value)

现在要做的是检查字符串是否为数组横幅,转换为:
'user1012'==='[object object]'

您可以使用array.prototype.includes方法测试给定值是否在数组中。includes将返回布尔值true或false

if(bannedUsers.includes(u.value){
返回警报('该用户名被禁止')
}

是的,我以前试过,这是我第一次试过的time@Yoimolk然后你应该编辑你的文章以使用.includes,这样我们就可以找出错误。使用
.includes
是“更正确的”。在验证上述内容并检查具有相同用户名的现有用户的同时,在服务器端执行此操作并进行ajax调用。这是否回答了您的问题?此外,请确保合法的单词没有被禁止!某些单词中有不好的单词,因此请不要阻止这些单词!返回警报调用是没有意义的
警报('xxxx');返回false;
它可以工作,但不是最好的选择。