In this tutorial, we will try to answer the following questions;
- What is IP address?
- Examples of valid IP addresses
- Examples of invalid IP addresses
- Code example of form validation in JavaScript
What is IP address?
IP address represents a unique string of numbers having four parts and each part is separated by a dot that identifies each unique computer with the help of Internet Protocol to communicate over a network. Each part of IP address can be from 0 to 255.
Examples of valid IP addresses
123.231.232.121, Valid because of all four parts are in between 0-255
0.0.04.1, Valid because of all four parts are in between 0-255
Examples of invalid IP addresses
1231.231.232.121, Invalid due to 1231 because it must be in between 0-255
60.0.260.1, Invalid due to 260 because it must be in between 0-255
Code of IP Address validation in JavaScript – Example of form validation in JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<script> var IP_U_ENTER; function IP_VALIDATION(IP_U_ENTER){ Valid_IP = false; ipParts = IP_U_ENTER.split("."
); if(ipParts.length==4){ for(i=0;i<4;i++){ TheNum = parseInt(ipParts[i]); if(TheNum >= 0 && TheNum <= 255){} else{break;} } if(i==4)Valid_IP=true; } alert(Congratulate: You have entered the Valid_IP); } </script> <form name="Test"> <input type="text" name="IP"> <input type="button" value="Test IP Address" onclick="IP_VALIDATION(document.Test.IP.value)"> </form> |
List of All Programs of Javascript : Click Here .