Below is the code snippet where I have written the code to have the IP address masked and validate it using jQuery. But, I am getting error message for valid IP addresses (like 222.222.222.222). Can someone please help?
//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;
});
});
You regex is missing its | characters ie.
Also note you could shorten this to
If you dont need to check it is a valid ip address between 0-255
also any \ needs to be escaped itself so it will be \
reference
UPDATE
OP showed fiddle and hadnt read above line also mask had spaces either side of the . so the regex was never valid as the mask added the spaces.
fixed Fiddle