So what the heck are you trying to do, actually? You're checking that the IP (which you have parsed into an array of four items) conforms to some restrictions you set. Let's assume that you want to only allow all IP addresses between 224.0.0.0 and 239.255.255.255, inclusive, and the two addresses 225.225.225.0 and 229.0.0.20 (like you initially said). How do you do this?
Firstly, you check whether the IP is between 224.0.0.0 and 239.255.255.255 inclusive. For that, one can easily see only the first number has to be checked. Therefore:
if(ipArray[0]<224||ipArray[0]>239)
return false;
But wait: if the IP address is either 225.225.225.0 or 229.0.0.20, we should not return false! So our last code snippet becomes:
if((ipArray[0]<224||ipArray[0]>239)&&v!="255.255.255.0"&&v!="229.0.0.20")
return false;
Notice that the ||
part at the beginning of the conditional is "parenthesised" (for lack of a better word). This is because if we don't do it, the Javascript interpreter will think we meant this:
if(ipArray[0]<224||(ipArray[0]>239&&v!="255.255.255.0"&&v!="229.0.0.20"))
return false;
This is because the &&
operator has a higher precedence (read that part of the article!) than the ||
operator.
So now, with our new-found knowledge, let's rewrite your original code:
function (v) {
if(v=="") return true;
if(v=="0.0.0.0") return false;
if(!v.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) return false;
ipArray = v.split(".");
//We don't need to check for NAN anymore because it matched your
//beautiful regex above
if((ipArray[0]<224||ipArray[0]>239)&&v!="255.255.255.0"&&v!="229.0.0.20")
return false;
return true; //if we've reached this point, the IP conforms to the restrictions!
}
Is your question answered? If so, don't forget to help Stack Overflow and marking this answer as accepted (green checkmark)!