How do you check whether a given IP is internal or not using only javascript?
For example if you are given an IP of 192.168.1.1 the script should validate this and alert if this is an internal or external IP.
If you mean private just make sure it's in one of the following ranges:
Private IP address ranges
The ranges and the amount of usable IP's are as follows:
10.0.0.0 - 10.255.255.255 Addresses: 16,777,216
172.16.0.0 - 172.31.255.255 Addresses: 1,048,576
192.168.0.0 - 192.168.255.255 Addresses: 65,536
Function like this should help:
function isPrivateIP(ip) {
var parts = ip.split('.');
return parts[0] === '10' ||
(parts[0] === '172' && (parseInt(parts[1], 10) >= 16 && parseInt(parts[1], 10) <= 31)) ||
(parts[0] === '192' && parts[1] === '168');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With