Reputation: 23
I want to validate broadcast and multicast IP address, i.e. the two IP addresses I am using: 255.255.255.0
or 229.0.0.20
.
I want to throw an alert while entering multicast or broadcast IP.
The following piece of code is working for multicast addresses, but how to validate for broadcast address?
[
'validate-mcast-ip',
'Please enter a valid multicast IP in this field.',
function(v) {
if (v == "") return true;
if (v == "0.0.0.0") return false;
ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
ipArray = v.split(".");
isIP = ipPattern.test(v);
if (isNaN(ipArray[0]) || ipArray[0] < 224 || ipArray[0] > 239) {
isIP = false;
}
for (i = 1; i < ipArray.length; i++) {
if (isNaN(ipArray[i]) || ipArray[i] > 255) {
isIP = false;
}
}
return isIP;
}
]
Upvotes: 2
Views: 1258
Reputation: 39542
A "simple" regex such as /^(2(?:2[4-9]|3[0-9]))\.([0-2]?[0-9]?[0-9])\.([0-2]?[0-9]?[0-9])\.([0-2]?[0-9]?[0-9])$/g
should do exactly what you want.
function validateMulticastIP(ip) {
return !!ip.match(/^(2(?:2[4-9]|3[0-9]))\.([0-2]?[0-9]?[0-9])\.([0-2]?[0-9]?[0-9])\.([0-2]?[0-9]?[0-9])$/g);
}
validateMulticastIP('2.3.5.6'); //false
validateMulticastIP('2'); //false
validateMulticastIP('230.3.5.6'); //true
validateMulticastIP('255.255.255.0'); //false
validateMulticastIP('229.0.0.20'); //true
Upvotes: 3
Reputation: 926
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)!
Upvotes: 0