Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Code of IP Address validation in JavaScript – Example of form validation in JavaScript

Code of IP Address validation in JavaScript – Example of form validation in JavaScript

In this tutorial, we will try to answer the following questions;

  1. What is IP address?
  2. Examples of valid IP addresses
  3. Examples of invalid IP addresses
  4. 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

<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>

Topic Covered

Code of IP Address validation in JavaScript – Example of form validation in JavaScript

  List of All Programs of Javascript : Click Here 
.

Prof. Fzal Rehman Shamil
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325