-1

我想在我的函数检查中添加 notallowedcharacters 的功能,我该怎么做?

var userinput = prompt('Input characters:');

var lowercase = userinput.toLowerCase();

var allowedcharacters = ["a", "b", "c", "d", "e", "f"]

var notallowedcharacters = ["g-z"] && ["!","@","#","€","£","$","%","&","?","*","/","\","|",";",":","<",">","[","]","{","-","_","^","~","`","±","§"]

function match(input, statement) {
    for (var i = 0; i < statement.length; i++) {
        if (input.indexOf(statement[i]) == -1) {
            return false;
        }
    }
    return true;
}

if (match(lowercase, allowedcharacters)){
  alert(true);
}
else{
  alert(false);
}
4

1 回答 1

0

最好的方法是使用正则表达式来检查要在函数中标记为非法的字符串、字符、模式。

在 Mozilla 文档和网络上,有很多关于如何构建表达式的示例和解释,如果您需要,它们可以像您想象的一样复杂,这就是为什么它们是事实上的标准这种东西。

https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

例如,这会捕获不匹配的电话号码,这些电话号码可以外推到您的用例中。

<button onclick="testInfo(document.getElementById('phone'));">Check</button>

var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{3}\1\d{4}/;  // RegEx Pattern
function testInfo(phoneInput){  
    var OK = re.exec(phoneInput.value);  
    if (!OK)  
      window.alert(RegExp.input + " isn't a phone number with area code!");  
    else
      window.alert("Thanks, your phone number is " + OK[0]);  
}  
于 2013-11-07T06:05:30.730 回答