[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vr / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / asp / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / qst / sci / soc / sp / tg / toy / trv / tv / vp / wsg / wsr / x] [Settings] [Search] [Home]
Board
Settings Home
/wsr/ - Worksafe Requests


Thread archived.
You cannot reply anymore.



Guys I need to make a script which will check if the IP is an IPv4 public address.
I need to crate an array out of several IPs and make the script return only public IPv4.
192.168.1.1
10.0.1.1
192.168.0.4
24.25.26.27
123.123.123.123
4.4.4.4
a.b.c.d
001:0db8:85a3:0000:0000:8a2e:0370:7334
These are the example IPs, how should I go about this with vanilla JS?
>>
Bump
>>
>>628666

Use a regular expression to check that they're all numbers

/(\d{1,3}\.){3}(\d{1,3})/

This is like a first test to try, it still accepts things like 300.300.100.1 (which is obviously not a valid IP). You can then split the 4 numbers and check that they're all in range

(From memory, I don't remember the exact names for this)

function checkValidIP(myip){
segments = myip.split(".")

(Pseudocode)
foreach s in segments:
...if s > 255:
......return false
return true
}
>>
>>628713
OP said "public IP", which means no multicast, no 192.168.x.x, etc.



Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.