javascript - Match IP with a range stored in an array - Stack Overflow

I am returning an IP and then paring it from a range.I am just looking at the US IP and if it falls in

I am returning an IP and then paring it from a range.

I am just looking at the US IP and if it falls in a range I am displaying a messge. I am getting the IP correctly but when I am trying to match it with the range I am getting a syntax error of unexpected range. How should I resolve this?

here is how my code looks like

$.getJSON("", function (data) {
    var x = data.ip;
    $('body').html(x);

    var usRange = [3.0.0.0, 222.229.21.255];
    if (x >= usRange[0] && x <= usRange[1]) {
        alert('US');
    } else alert('outside US');
});

Here is my fiddle

I am returning an IP and then paring it from a range.

I am just looking at the US IP and if it falls in a range I am displaying a messge. I am getting the IP correctly but when I am trying to match it with the range I am getting a syntax error of unexpected range. How should I resolve this?

here is how my code looks like

$.getJSON("http://jsonip.", function (data) {
    var x = data.ip;
    $('body').html(x);

    var usRange = [3.0.0.0, 222.229.21.255];
    if (x >= usRange[0] && x <= usRange[1]) {
        alert('US');
    } else alert('outside US');
});

Here is my fiddle

Share Improve this question edited Nov 30, 2013 at 18:53 Praveen 56.6k35 gold badges136 silver badges166 bronze badges asked Nov 30, 2013 at 17:59 soumsoum 1,1593 gold badges21 silver badges49 bronze badges 11
  • And why do you think the built in javascript operators will work with IP adresses ? – adeneo Commented Nov 30, 2013 at 18:00
  • 2 And what I'm saying is that 3.0.0.0 isn't a valid number in javascript, so paring it against, well anything, is going to fail. – adeneo Commented Nov 30, 2013 at 18:07
  • 1 You see, parsing IP adresses is plicated, it's not something I'll just whip up in a minute, and it's not something you can do with javascript operators, as a number (float) can only have one decimal, and IP adresses are invalid as number, they have to be strings, and then you have to build a parser to pare them. – adeneo Commented Nov 30, 2013 at 18:09
  • 1 Just check if you can do it with regex – sSaroj Commented Nov 30, 2013 at 18:18
  • 1 why don't you just use an IP location service API that will tell you the country? – charlietfl Commented Nov 30, 2013 at 18:39
 |  Show 6 more ments

2 Answers 2

Reset to default 3

What the error means, you can't assign number with 4 decimal dots.

var usRange = [3.0.0.0, 222.229.21.255]; //Not possible

FYI: The IP address returned from the json is string, not a number.

So you approach won't work. Here check my approach,

1) Split the ip address based on . returned by $.getJSON

var x = data.ip.split('.');  // returns ["122", "164", "17", "211"]

2) Instead of using usRange array just do it with simple parison operations.

3) You cannot pare strings with numbers, so convert those strings to numbers like

+x[0] //convert "122" to 122

Finally,

$.getJSON("http://jsonip.", function (data) {
    var x = data.ip.split('.');
    if (+x[0] >= 3 && +x[0] <= 222) {
        if (+x[1] >= 0 && +x[1] <= 229) {
            if (+x[2] >= 0 && +x[2] <= 21) {
                if (+x[3] >= 0 && +x[3] <= 255) {
                    alert("Within range");
                }
            }
        }
    } else {
        alert("Is not within range");
    }
});

JSFiddle

I decided to rewrite my answer here for the sake of a better method. The first method actually converts the IP addresses to integers using bit shifting and then pares the integer values. I've left the second answer as an option but I prefer the first.

Neither of these functions will validate your IP addresses!

Method 1: Bit Shifting

/**
 * Checks if an IP address is within range of 2 other IP addresses
 * @param  {String}  ip         IP to validate
 * @param  {String}  lowerBound The lower bound of the range
 * @param  {String}  upperBound The upper bound of the range
 * @return {Boolean}            True or false
 */
function isWithinRange(ip, lowerBound, upperBound) {

  // Put all IPs into one array for iterating and split all into their own 
  // array of segments
  var ips = [ip.split('.'), lowerBound.split('.'), upperBound.split('.')];

  // Convert all IPs to ints
  for(var i = 0; i < ips.length; i++) {

    // Typecast all segments of all ips to ints
    for(var j = 0; j < ips[i].length; j++) {
      ips[i][j] = parseInt(ips[i][j]);
    }

    // Bit shift each segment to make it easier to pare
    ips[i] = 
      (ips[i][0] << 24) + 
      (ips[i][1] << 16) + 
      (ips[i][2] << 8) + 
      (ips[i][3]);
  }

  // Do parisons
  if(ips[0] >= ips[1] && ips[0] <= ips[2]) return true;

  return false;
}

Method 2: Plain 'Ol Logic Mess

/**
 * Checks if an IP address is within range of 2 other IP addresses
 * @param  {String}  ip         IP to validate
 * @param  {String}  lowerBound The lower bound of the range
 * @param  {String}  upperBound The upper bound of the range
 * @return {Boolean}            True or false
 */
function isWithinRange(ip, lowerBound, upperBound) {

  // Save us some processing time if the IP equals either the lower bound or 
  // upper bound
  if (ip === lowerBound || ip === upperBound) return true;

  // Split IPs into arrays for iterations below. Use same variables since 
  // we won't need them as strings anymore and because someone will plain 
  // about wasting memory.
  ip = ip.split('.');
  lowerBound = lowerBound.split('.');
  upperBound = upperBound.split('.');

  // A nice, classic for loop iterating over each segment in the IP address
  for (var i = 0; i < 4; i++) {

    // We need those segments to be converted to ints or our parisons 
    // will not work!
    ip[i] = parseInt(ip[i]);
    lowerBound[i] = parseInt(lowerBound[i]);
    upperBound[i] = parseInt(upperBound[i]);

    // If this is our first iteration, just make sure the first segment 
    // falls within or equal to the range values
    if (i === 0) {
      if (ip[i] < lowerBound[i] || ip[i] > upperBound[i]) {
        return false;
      }
    }

    // If the last segment was equal to the corresponding lower bound 
    // segment, make sure that the current segment is greater
    if (ip[i - 1] === lowerBound[i - 1]) {
      if (ip[i] < lowerBound[i]) return false;
    }

    // If the last segment was equal to the corresponding upper bound 
    // segment, make sure that the current segment is less than
    if (ip[i - 1] === upperBound[i - 1]) {
      if (ip[i] > upperBound[i]) return false;
    }
  }

  return true;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744929234a4601629.html

相关推荐

  • javascript - Match IP with a range stored in an array - Stack Overflow

    I am returning an IP and then paring it from a range.I am just looking at the US IP and if it falls in

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信