Validate an IP(Internet Protocol) address
by GOKILAVANI[ Edit ] 2014-05-02 09:14:56
Validate an IP address using JavaScript
function ValidateIPaddress(inputText)
{
var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if(inputText.value.match(ipformat))
{
document.form1.text1.focus();
return true;
}
else
{
alert("
You have entered an invalid IP address!");
document.form1.text1.focus();
return false;
}
}
Example of valid IP address
115.42.150.37
192.168.0.1
110.234.52.124
Example of invalid IP address
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digit has allowed
255.0.0.y – only digit has allowed
666.10.10.20 – digit must between [0-255]
4444.11.11.11 – digit must between [0-255]
33.3333.33.3 – digit must between [0-255]