简体   繁体   English

验证JavaScript

[英]Validate JavaScript

I am trying to validate 4 of these addresses. 我正在尝试验证其中4个地址。 I tried multiple ways and they do not seem to be working. 我尝试了多种方法,但它们似乎没有用。 If I use a single address to compare it with, it works but multiple dont. 如果我使用一个地址进行比较,则可以使用,但不能使用多个地址。

function validateAddress() {
    var address = document.forms["ipForm"]["address"].value;
    var error = "";
    var ip1 = "192.168.0.0";
    var ip2 = "192.168.0.1";
    var ip3 = "192.168.0.2";
    var ip4 = "192.168.0.3";


    if (!address.match(ip1)) {
        error += "Please enter a valid address \n";
    }

    else if (!address.match(ip2)) {
        error += "Please enter a valid address \n";
    }

    else if (!address.match(ip3)) {
        error += "Please enter a valid address \n";
    }

    else if (!address.match(ip4)) {
        error += "Please enter a valid address \n";
    }
    return error;
}

Don't need match at all if you are not validating against a regEx. 如果您不针对regEx进行验证,则完全不需要match If there is just a set of valid options, then just test against the set with Array.indexOf 如果只有一组有效的选项,则使用Array.indexOf测试

var input = "xyz"// Some user input 
var validIps = ["192.168.0.0", "192.168.0.1", "192.168.0.2", "192.168.0.3"];

if (validIps.indexOf(input) == -1) { // if this is not in set indexOf returns -1
  // error
}

Use indexOf: 使用indexOf:

if (ip1.indexOf(address) === -1) { 
     error += " Please enter a valid address \n"; 
} else if (ip2.indexOf(address) === -1) { 
     error += "Please enter a valid address \n"; 
} else if (ip3.indexOf(address) === -1) {
     error += "Please enter a valid address \n"; 
} else if (ip4.indexOf(address) === -1) {
    error += "Please enter a valid address \n"; 
}

You could also make it much shorter by creating an array with the 4 IP addresses and then calling: 您还可以通过使用4个IP地址创建一个数组,然后调用以下命令来使其更短:

var array = ["192.168.0.0","192.168.0.1","192.168.0.2","192.168.0.3"];
if (array.indexOf(address) === -1) {
    //not valid
}

Looks like a good example for Array.prototype.some 看起来是Array.prototype.some一个很好的例子

 var address = document.forms["ipForm"]["address"].value, error, ips = ["192.168.0.0", "192.168.0.1", "192.168.0.2", "192.168.0.3"], match = ips.some(function(ip) { return address.match(ip) !== null; }); if(!match){ error = "Please enter a valid address"; } 

You need to revisit your logic. 您需要重新审视自己的逻辑。 You will always get an error message returned. 您将始终收到一条错误消息。 Let's say our address var is "bob". 假设我们的address var是“ bob”。 It does not match ip1 so the condition is true and we set the error message. 它与ip1不匹配,因此条件为true,我们设置了错误消息。 Let's say our address var is "192.168.0.0". 假设我们的地址var是“ 192.168.0.0”。 It does match ip1 so the condition of the first if statement will be false (because of the negation with ! ). 它确实与ip1匹配,因此第一个if语句的条件将为false(因为与!的取反)。 We will go to the else if condition and try to match on ip2 . 如果有条件,我们将转到else并尝试在ip2上进行匹配。 Does it match? 匹配吗? No, so the condition will be true and the error message will be set. 否,因此条件将为true,并且将设置错误消息。 You want to try something like 您想尝试类似

if(!address.match(ip1) && !address.match(ip2) && !address.match(ip3) && !address.match(ip4)) 
    { error += "Please enter a valid address \n";   }

Which is essentially: if address doesn't match ip1, 2, 3 or 4, then set the error message. 本质上是这样:如果地址与ip1、2、3或4不匹配,则设置错误消息。

You might also want to explore the possibility of having the allow list stored in an array which you can iterate over. 您可能还想探索将允许列表存储在可以迭代的数组中的可能性。 It would mean you wouldn't need another if statement or update your condition each time you add a new ip address to the allow list. 这意味着每次将新的IP地址添加到允许列表时,您都不需要其他if语句或更新条件。 With each iteration you can test the input address against each item in your array. 每次迭代时,您都可以针对数组中的每个项目测试输入地址。 If it matches you can return from the function early. 如果匹配,您可以尽早从函数中返回。 If it does not match you can return the error message after you're done looping. 如果不匹配,则可以在完成循环后返回错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM