JavaScript 验证

<html>
<title>www.mimvp.com</title>

<script>
	// IP是否合法, false - 不合法; true - 合法
	function valid_ip(ip) {     
		var reg = /^(\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])$/;     
		return reg.test(ip);     
	}  

	var ip_list = ['115.29.237.28', '123.57.78.100', '265.100.1.1', '010.100.1.1', '192.10.1'];
	for(var i=0; i<ip_list.length; i++) {
		var ip = ip_list[i];
		if(valid_ip(ip)) {
			document.write(ip + "  ->  " + valid_ip(ip) + "<br>");		
		} else {
			document.write(ip + "  ->  " + valid_ip(ip) + "<br>");	
		}
	}
</script>

</html>

输出结果:

115.29.237.28 -> true
123.57.78.100 -> true

265.100.1.1 -> false
010.100.1.1 -> false
192.10.1 -> false