0

I am trying to validate 4 of these addresses. 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;
}
1
  • What do you mean "multiple don't"? Why should an address match more than one ip? Commented Nov 15, 2015 at 21:47

4 Answers 4

1

Don't need match at all if you are not validating against a regEx. If there is just a set of valid options, then just test against the set with 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
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use 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:

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
}

Comments

0

Looks like a good example for 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";
  }

3 Comments

@webdeb, for some reason I thought there was an ip reference but it seems he was using address.
Sorry, where does some come from?
It's from Array.prototype.some.
0

You need to revisit your logic. You will always get an error message returned. Let's say our address var is "bob". It does not match ip1 so the condition is true and we set the error message. Let's say our address var is "192.168.0.0". It does match ip1 so the condition of the first if statement will be false (because of the negation with !). We will go to the else if condition and try to match on ip2. Does it match? No, so the condition will be true and the error message will be set. 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.