function checkIp()
{
str=document.form1.txtIP.value;
if(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(str)==false)
{
window.alert('错误的IP地址格式');
document.RegExpDemo.txtIP.select();
document.RegExpDemo.txtIP.focus();
return;
}
if(RegExp.$1<1 || RegExp.$1>254||RegExp.$2<0||RegExp.$2>254||RegExp.$3<0||RegExp.$3>254||RegExp.$4<1||RegExp.$4>254)
{
window.alert('错误的IP地址');
document.RegExpDemo.txtIP.select();
document.RegExpDemo.txtIP.focus();
return;
}
//剔除 如  010.020.020.03 前面 的0 
var str=str.replace(/0(\d)/g,"$1");
str=str.replace(/0(\d)/g,"$1");
window.alert(str);
}

解决方案 »

  1.   

    <SCRIPT LANGUAGE="JavaScript">
    function isip(s){
    var check=function(v){try{return (v<=255 && v>=0)}catch(x){return false}};
    var re=s.split(".")
    return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false
    }var s="202.197.78.129";
    alert(isip(s))
    </SCRIPT>
      

  2.   

    Expression:  ^(\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])$  
    Rating: 5  
    Description:   This matches an IP address, putting each number in its own group that can be retrieved by number. If you do not care about capturing the numbers, then you can make this shorter by putting everything after ^ until immediately after the first \. in a group ...  
    Matches:  [0.0.0.0], [255.255.255.02], [192.168.0.136]  [ More Details]  
    Non-Matches:  [256.1.3.4], [023.44.33.22], [10.57.98.23.]