12

I have a function which get a dot seperated string and parse it to array. And I want to loop these array elements and check a value is greater than 255 and return false, if not continue to function statements and return true as end of function. But it never stops the loop.. and always return true.

Here is code:

checkipAddress = function(value){//instance value: 999.999.999.999 result:true
        debugger
        var array = value.split('.');
        $.each(array, function(index,val){
            debugger
            if(parseInt(val)>255)
                return false; // it should end the loop and exit function with return false.
        });
        return true;
    }
2
  • 4
    When you say return false; within the $.each statement, you're returning from just that function and not checkipAddress. Commented May 6, 2015 at 12:09
  • @jrad I got the point now :) thank you Commented May 6, 2015 at 12:28

1 Answer 1

15

Returning from one function doesn't magically make the one that called it end, much less use a specific return value.

If you want to do that, you have to set a variable that the outer function will use:

checkipAddress = function(value){
    var rv = true; // <=== Default return value
    var array = value.split('.');
    $.each(array, function(index,val){
        if(parseInt(val)>255)
            return rv = false; // <=== Assigns false to `rv` and returns it
    });
    return rv; // <=== Use rv here
}

Side note: Your function will happily allow IP strings like "0.-234343.-1.0" and "1foo.2foo.3foo.4foo". You might consider:

checkipAddress = function(value){
    var rv = true; // <=== Default return value
    var array = value.split('.');
    $.each(array, function(index,str){
        str = $.trim(str);
        var val = +str;
        if(!str || val < 0 || val > 255)
            return rv = false; // <=== Assigns false to `rv` and returns it
    });
    return rv; // <=== Use rv here
}

That's a bit better, but it also doesn't check whether there are exactly four parts to the IP, and allows values like "1.1e2.3.4" (exponent notation). And all of this is, of course, specific to IPv4, whereas we're entering an IPv6 world...

Sticking with IPv4, though, if your goal is to ensure that it's a four-part IPv4 address in normal form, I'd plump for regex:

checkipAddress = function(value){
    var rv;
    var match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(value);
    var n, part;
    if (match) {
        rv = true;
        for (n = 1; rv && n < 5; ++n) {
            part = +match[n]; // We know it's not blank from the regex
            if (part > 255) { // We know it's not negative or fractional from the regex
                rv = false;
            }
        }
    } else {
        rv = false;
    }
    return rv;
}

Or on modern browsers (or using a decent Array#every shim):

checkipAddress = function(value){
    var match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(value);
    rv = !match ? false : Array.prototype.slice.call(match, 1).every(function(entry) {
        // We know it's not blank, negative, or fractional from the regex
        return +entry <= 255;
    });
    return rv;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why would you use return inside the callback of each? It doesn't make sense ...
@devnull69: It stops the $.each loop.
@T.J.Crowder I tried this and works fine.. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.