下面是我编写的代码片段,用于屏蔽 IP 地址并使用 jQuery 对其进行验证。但是,我收到有效 IP 地址(如 222.222.222.222)的错误消息。有人可以帮忙吗?
//Validate the form
$('#form').validate({
rules: {
ip: {
required: true,
IP4Checker: true
},
subnet: {
required: true,
IP4Checker: true
},
gateway: {
required: true,
IP4Checker: true
},
dns1: {
required: true,
IP4Checker: true
},
dns2: {
required: true,
IP4Checker: true
}
},
messages: {
ip: "Please enter a valid IP Address",
subnet: "Please enter a valid Subnet Mask Address",
gateway: "Please enter a valid Gateway Address",
dns1: "Please enter a valid DNS1 Address",
dns2: "Please enter a valid DNS2 Address"
}
});
//Validate the IP addresses
$(function() {
$.validator.addMethod('IP4Checker', function(value) {
var ip = "^(?:(?:25[0-5]2[0-4][0-9][01]?[0-9][0-9]?)\\.){3}" +
"(?:25[0-5]2[0-4][0-9][01]?[0-9][0-9]?)$";
return value.match(ip);
});
});
//Set mask for IP address fields
$(".ip").mask("999 . 999 . 999 . 999");
//Store numbers in hidden field
$(".ip").blur(function () {
//Create char array from phone number field
var charArray = $(this).val().split("");
var num = "";
//taking the input
$.each(charArray, function(index, value) {
num = num + value;
});
});