jquery每个循环返回false而不是结束函数

时间:2022-12-08 19:57:22

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.

我有一个函数,它获得一个点分隔字符串并将其解析为数组。我想循环这些数组元素并检查一个大于255的值并返回false,如果没有继续执行函数语句并返回true作为函数结束。但它永远不会停止循环..并始终返回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;
    }

1 个解决方案

#1


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:

旁注:您的函数将很乐意允许IP字符串,如“0.-234343.-1.0”和“1foo.2foo.3foo.4foo”。你可能会考虑:

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...

这有点好,但它也不会检查IP是否正好有四个部分,并且允许使用类似“1.1e2.3.4”(指数表示法)的值。当然,所有这一切都是针对IPv4的,而我们正在进入IPv6世界......

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:

但是,坚持使用IPv4,如果您的目标是确保它是正常形式的四部分IPv4地址,那么我会为正则表达式做好准备:

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;
}

#1


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:

旁注:您的函数将很乐意允许IP字符串,如“0.-234343.-1.0”和“1foo.2foo.3foo.4foo”。你可能会考虑:

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...

这有点好,但它也不会检查IP是否正好有四个部分,并且允许使用类似“1.1e2.3.4”(指数表示法)的值。当然,所有这一切都是针对IPv4的,而我们正在进入IPv6世界......

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:

但是,坚持使用IPv4,如果您的目标是确保它是正常形式的四部分IPv4地址,那么我会为正则表达式做好准备:

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;
}