IP格式正则验证
285 views
0
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
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2015-07-28 03:26:02
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: IP格式正则验证 (米扑博客)