r0skar
r0skar

Reputation: 8696

jQuery+Check if a array contains only IP adresses

I have an array like this (output from console.log):

["122.42.123.1", "122.42.123.1", "122.42.123.1", "awdawd", "awdawd"]

How could I check if it contains only IP addresses and if so return true? And in case it doesnt (like in the example above) return false?

I have tried various functions I found here and on Google (like this one) and used them in a each loop, but that doesnt work:

//Get ip list from a textarea (each row = 1 ip)
var content = $("#banUList").val();
var ips = content.split("\n");
console.log(ips);

$(ips).each(function(){

    if(checkFunction($(this)) == false){
        //Wrong
    }else{
        //correct
    }

});

If possible I would like to avoid this loop at all, and have a function that checks the whole array for any var that is not an IP. No matter how many vars there are.

Upvotes: 0

Views: 1035

Answers (5)

mplungjan
mplungjan

Reputation: 178011

How about this version which does NOT use a loop DEMO

// regex from http://tools.netshiftmedia.com/regexlibrary/
var re = /((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})/g; 

function isArrIpOnly(arr) {
  return arr.toString().match(re).length==arr.length;
}


var arr = ["122.42.123.1", "122.42.123.1", "122.42.123.1", "awdawd", "awdawd"];   

alert(isArrIpOnly(arr));

Upvotes: 0

gdoron
gdoron

Reputation: 150263

function isValidIp(value){
    // The code you got from google
    return true/false;
}

var valids = [];

$.each(ips, function(){
  if (isValidIp(this))  
      valids.push(this);
});

Or with the jQuery grep util:

var valids = $.grep(ips, function(element){
                 return isValidIp(element);
             });   

If you just want to get true\false when all the address are valid\invalid:

var areAllValid = $.grep(ips, function(element){
                        return !isValidIp(element);
                     }).length === 0;

Upvotes: 1

Martin Jespersen
Martin Jespersen

Reputation: 26183

This function will do as you wish:

function ipOnly(arr) {
    for(var i=0,l=arr.length;i<l;i++)
        if(!/^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$/.test(arr[i])) 
            return false;
    return true;
}

fiddle: http://jsfiddle.net/DWQzm/

Upvotes: 4

jbabey
jbabey

Reputation: 46647

You can use a regex to validate the IP address:

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

http://www.regular-expressions.info/examples.html

CHALLENGE: someone more experienced with regex than myself can probably write a multi-line aware regex that can validate the entire textarea in one sweep without using any looping.

Upvotes: 1

Yogu
Yogu

Reputation: 9445

It is simply impossible to check all entries of a list without using a loop. To complete your source code:

function arrayConsistsOfIPAddresses(ips) {
   var result = true;
   $(ips).each(function() {
     result &= isIPAddress($(this));
   }
   return result;
}

You can use the regex posted by jbabey to implement the function isIPAddress:

function isValidIPAddress(value) {
    var ipRegex = /b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/b
    return value.match(ipRegex); 
}

Upvotes: 1

Related Questions