Error jquery each loop return false not end the function

0 votes

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 loopand 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;
    }
Jun 2, 2020 in JQuery by kartik
• 37,530 points
2,520 views

1 answer to this question.

0 votes

Hii @kartik,

Returning from one function doesn't call as it's 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
}

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
}

Hope it helps!!

answered Jun 2, 2020 by Niroj
• 82,820 points

Related Questions In JQuery

0 votes
1 answer

Uncaught ReferenceError:Karma: jQuery is not defined

Hii @kartik, You first have to load jQuery ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,820 points
2,215 views
0 votes
1 answer

How to remove close button on the jQuery UI dialog?

Hii, I found this worked for me in the end ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,820 points
2,139 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7, 2020 in JQuery by Amit
• 140 points
808 views
0 votes
1 answer
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,820 points
11,558 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,820 points
743 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,820 points
2,283 views
0 votes
1 answer

How to set cache false for getJSON in jQuery?

Hello @kartik, Your code just needs a trigger ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,820 points
199 views
0 votes
1 answer

Error:jQuery scrollTop not working in Chrome but working in Firefox

Hello @kartik, If your CSS html element has the following overflow markup, scrollTop will ...READ MORE

answered May 29, 2020 in JQuery by Niroj
• 82,820 points
3,678 views
0 votes
1 answer

What is the equivalent of jQuery .hide() to set visibility: hidden

Hello Kartik, There isn't one built in but ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,820 points
2,150 views