javascript IP 正则表达

时间:2022-01-24 14:53:08

可以校验你在你的文本框上输入的数据是否是正确Ip

function check_ip()
{   
    var i;
    var ip=document.getElementById('Ip').value;
    var node = document.getElementById("ip_span");
    i = ip.length;
    if(i==0)
    {
        alert("警告:请输入IP地址");
    }
    alert(ip);
    var re=/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/ ;
    if(re.test(ip))
    {
        node.firstChild.nodeValue = "Ip地址输入正确";
    }
    else
    {
        node.firstChild.nodeValue = "非法Ip地址";
    }
}