var exp=/^(\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])$/;
if(userip.match(exp) == null) {
alert("请输入正确的IP地址!");
return;
}上面的代码中,userip就是指定的要验证的IP地址!
var exp=/^(\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])$/;
if(userip.match(exp) == null) {
alert("请输入正确的IP地址!");
return;
}上面的代码中,userip就是指定的要验证的IP地址!
本文标题:验证一个字符串是否为IPv4格式的IP地址的JS代码
本文链接:https://www.haomeiwen.com/subject/mciopxtx.html
网友评论