2

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;
  }
]
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
  • the above piece of code is is working for multicaste ip but how can i validate for broadcaste address – user2310261 Apr 23 '13 at 07:48
  • Please see this piece of code for braod case if(isNaN(ipArray[0]) || ipArray[0] < 224 || ipArray[0] > 239){ isIP = false; } – user2310261 Apr 23 '13 at 07:49

2 Answers2

3

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

Regex101 Demo

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

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)!

tomsmeding
  • 916
  • 7
  • 25
  • Yes that what i want...let me try the above code i am able to throw an error for ip 225.225.225.0 but not for 229.0.0.20 – user2310261 Apr 23 '13 at 08:43
  • if(isNaN(ipArray[0]) || ipArray[0] < 224 || ipArray[0] > 239 && v!="225.225.225.0" && v!="229.0.0.20"){ isIP = false; } this is what i tried but in case 229.0.0.20 its not showing error...but it should throw an error – user2310261 Apr 23 '13 at 08:53
  • Don't forget the extra parentheses... Use `if((isNAN(ipArray[0])||ipArray[0]<224||ipArray[0]>239)&&v!="225.225.225.0"&&v!="229.0.0.20")` as your if-statement. See whether that works. (Typing error firstly) – tomsmeding Apr 23 '13 at 08:55
  • yes i have written it corectly but why this code is not working i dont know if((isNaN(ipArray[0])<224||ipArray[0]>239) && v!="225.225.225.0" && v!="229.0.0.20") { isIP=false; } – user2310261 Apr 23 '13 at 09:10
  • Please copy-paste my code instead of re-typing it. The code you gave is incorrect, look at the `isNAN(ipArray[0])||ipArray[0]<224` bit. Also, please place code between backticks, like \`this\` (becomes `this`). (Not the apostrophe but the one on the keyboard actually pointing left) – tomsmeding Apr 23 '13 at 09:25
  • i should not allow ip between 224 to 239 and should throw an error ... if(isNaN(ipArray[0]) || ipArray[0] < 224 && ipArray[0] > 239){ isIP = false; } is that correct please help me – user2310261 Apr 23 '13 at 13:16