Question:
How to check if IP Address is correct by using Regular Expressions in JavaScript?
Ranimi23 r
2007-09-06 13:35:57 UTC
I have a TextBox in my HTML page. User is going to enter IP Address and I wanna check it while the user is inserting the digits and not at the end of all address.

If he wrote 3. = Bad IP, can't start with 3
if he wrote 0.0. = Maybe a start of a good IP
if he wrote 255.256. = Bad

The right address can be between 0.0.0.0 and 255.255.255.255

The big problem is to write the Regular Expression that is going to check if the first digit is 0 or 1 or 2, and after it can be not more than two digits and then "." (dot) and more...
Three answers:
AQuestionMark
2007-09-06 21:32:32 UTC
Yeah, it isn't that easy, when using RegExp to check, first split the ip by dot, then check the numbers with a loop, if all number ok, is a valid address otherwise, no.

Here's a demo http://www.freewebs .com/swhtam/ YA/RegExpIPcheck.html go to menu View, source for the complete code.

In the demo, I put up another option to check the ip, cause personally i perfer number check against number, check it out see if you agree with me.

BTW, FYI class A (very large networks) ip address allow first octet from 1-126.



Please note that i put 2 space in the link above to get around the 999 page error, so take the space out to use the link thanks
?
2016-11-14 14:09:01 UTC
while you are trying out a service working regionally on the device working the assessments, then use the loopback ip address (127.0.0.a million) as a replace of the ip gained out of your dhcp/assigned. while you're submitting that archives to some form or something.... that doesn't make experience, the service can retrieve the distant ip address, alter your server element if achieveable.
Mak Forest
2007-09-06 14:07:28 UTC
Try this function:









Good luck!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...