<script language=javascript>
function checkIP2(sIPAddress) 

var exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/; 
var reg = sIPAddress.match(exp); 
var ErrMsg="你输入的是一个非法的IP地址段!\nIP段为::xxx.xxx.xxx.xxx(xxx为0-255)!" 
var Msg="你输入的是一个合法的IP地址段!" 
if(reg==null) 

alert(ErrMsg); 

else 

alert(reg); 

} checkIP2(prompt("请输入要检测的IP",""));
</script>

解决方案 »

  1.   

    function verifyDottedIP(cntrl,errmsg1,errmsg2,flag) 
    {
        ip_str = cntrl.value;
    //alert(ip_str);

    if (ip_str == "") {
     if (flag == 1){
    //window.alert("Port can not be empty.");
    window.alert(errmsg1);
    cntrl.select();
         return false;
    }
               return true;
    }else{
    var val = 0;
    var i = index = 0;

    while (index < ip_str.lastIndexOf(".")) {
    k = index;
    index = ip_str.indexOf(".", index);
    val = toNumber(ip_str, k, index); if (val < 0 || val > 255)
    break;
    i++; index++;
    } if (i == 3) {
    if (index == ip_str.length) {
    i = 2;
    } else {
    val = toNumber(ip_str,index, ip_str.length);
    if (val < 0 || val > 255) i = 2;
    }
    } if (i != 3) {
    alert(errmsg2);
    cntrl.select();
    return false;
    }
    }
    return true;
    }
      

  2.   

    function isIP(sIP) {
        sIP = String(sIP).replace(/^\s+|\s+$/g,"") // trim
    // remove non numeric characters and dot
        // sIP = String(sIP).replace(/([^0-9.]|)/g,"") 
        var result = sIP.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
        return (result && 
                result.length == 5 && 
                isFF(result[1]) && 
                isFF(result[2]) && 
                isFF(result[3]) &&  
                isFF(result[4]));
    }
      

  3.   

    function isFF(x) {
        return (!isNaN(x) && x >= 0 && x <= 255)
    }