日期:2014-05-16  浏览次数:20305 次

JS 正则表达式验证 IP

Javascript正则表达式验证IP

?

<script language=javascript>

function check()

{

?

//alert("IP地址输入非法!");

var pattern=/^(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])$/;

flag_ip=pattern.test(document.all.ip.value);

if(!flag_ip)

{

alert("IP地址输入非法!");

document.all.ip.focus();

return false;

}

}

</script>

<form onsubmit="return check()">

? ? <input name="ip" type="text" /> <input type="submit" value="Submit Query" />

</form>