使用正则表达是验证IP格式:
js代码:
var iP = '192.1688.2.4';
function checkIP(ip)
{
let formIP = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//使用正则表达式预定义要校验的格式:
if(formIP.test(ip))//使用RegExp对象的test方法校验;
{
if(RegExp.$1 <= 255 && RegExp.$1 >= 0 && RegExp.$2 <= 255 && RegExp.$2 >= 0 && RegExp.$3 >= 0 && RegExp.$3<= 255 && RegExp.$4 >= 0 && RegExp.$4 <= 255)
{
return true;
}else{
alert('格式错误');
return false;
}
}else{
alert('error');
return false;
}
}
一般用于表单输入的验证。