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,510 points
7,299 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,880 points

Related Questions In JQuery

0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

We can fix this error by using jQuery() . ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,670 points
5,041 views
0 votes
1 answer

$ is not a function - jQuery error

The typeerror: $ is not a function ...READ MORE

answered Jun 6, 2022 in JQuery by Edureka
• 13,670 points
873 views
0 votes
1 answer

Why does jQuery or a DOM method such as getElementById not find the element?

At the moment the script is executed, ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,670 points
1,552 views
0 votes
1 answer

jQuery UI " $("#datepicker").datepicker is not a function"

The "$(...).datepicker is not a function" jQuery ...READ MORE

answered Jun 6, 2022 in JQuery by Edureka
• 13,670 points
10,931 views
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,880 points
22,476 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,880 points
2,990 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,880 points
4,688 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,880 points
717 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,880 points
14,109 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,880 points
13,204 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP