不废话,我这个起码不坑人,有的把我坑死

var objRegExp = /^((([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))\.)((([0-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))\.){2}(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))$/;

/^(第一段+第二段+第三段+第四段)&/

尤其注意最后一段不需要.

第一段:1-255  (([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))

第二段:0-255  (([0-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))

第三段:0-255  (([0-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))

第四段:1-255  (([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))

本人js完整代码与图片

<script>
        function Check() {
            var Result = false;
            var txtUrl = $("#txtUrl").val();
            var objRegExp = /^((([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))\.)((([0-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))\.){2}(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))$/;           
            if (txtUrl != "" && txtUrl != undefined) {
                if (txtUrl.indexOf("#") == -1) {  
                    if (objRegExp.test(txtUrl)) {
                        Result = true;
                    }
                    else {
                        alert(txtUrl + " 属于非法IP");
                        Result = false;
                    }
                }
                else {
                    var txtUrls = txtUrl.split("#");
                    for (var i = 0; i < txtUrls.length; i++) {
                        if (objRegExp.test(txtUrls[i])) {
                            Result = true;
                        }
                        else {
                            alert(txtUrls[i] + " 属于非法IP");
                            Result = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                alert("提交IP不能为空!");
                Result = false;
            }
            return Result;
        }
    </script>

正则表达式 IP域名-LMLPHP

每一段结尾是 . 需要特殊编译 反斜杠  \.

{2} 表示第二段是是一模一样的的   两短,不嫌累可以分开写

正则表达式 IP域名-LMLPHP

05-11 20:28