<html>
<head>
<title>最简洁的IP判断正则表达式</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<script type="text/javascript" src="http://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"></script>
</head>
<body>
IP字段: <input type="text" id="ip" />
判断结果:<input type="text" id="result" />
<script type="text/javascript">
$(document).ready(function(){
function validateIP(str){
return !!str.match(/^(([-]|[-]\d|[]?\d\d?)($|(?!\.$)\.)){}$/);
}
$("#ip").blur(function(){
var m = validateIP($(this).val());
m = m || 'null';
$("#result").val(m);
});
});
</script>
</body>
</html>
结果:
IP字段: 判断结果: