Skip to content
Related Articles

Related Articles

Improve Article

How to check validation of IP Address in jQuery ?

  • Difficulty Level : Hard
  • Last Updated : 12 May, 2021

In this article, we will find how to validate the IP Address using jQuery.

IP Address: Every computer connected to the Internet is identifiable by a unique string, which is known as its Internet Protocol (IP) address. It consists of four numbers (each between 0 and 255) separated by dots. The format of an IP address can be written as four decimal numbers separated by dots, each number can be written from 0 to 255. An example of valid IP address is as follows.

  • 255.255.11.135
  • 110.234.52.124

We can check whether the IP Address is valid or not using regex (regular expression) easily. Let us take an example to understand how it works. Now let us first discuss how regex can be used to validate IP addresses. The regular expression that would be used to verify IP address specifically ip4 address is as below.

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

  • In the above expression [0-9] indicates any number from 0 to 9.
  •  | indicates conditional “or“.
  • [1-9][0-9] indicates any number from 10 to 99.
  • 1[0-9]{2} means 1[0-9][0-9] which indicates any number starting from 100 to 199.
  • 2[0-4][0-9] indicates any number starting from 200 to 249.
  • 25[0-5] indicates any number starting from 250 to 255.
  • {3} indicates that the pattern occurs three times.

Example:



HTML




<!DOCTYPE html>
<html lang="en">
  <head>
    <link
      rel="stylesheet"
      href=
    <script src=
    </script>
    <script src=
    </script>
  </head>
  <body style="text-align: center">
    <h2>Jquery IP Address Checker</h2>
    <div>
      <div class="form-group row">
        <label for="inputPassword"
               class="col-sm-2 col-form-label">
          Enter IP Address:
        </label>
        <div class="col-sm-8">
          <input
            type="text"
            class="form-control"
            id="ip"
            name="ip"
            placeholder="IP Address"
          />
        </div>
      </div>
      <button type="submit" id="submit" 
              class="btn btn-primary">
        Submit
      </button>
      <p style="color: green" id="demo"></p>
  
    </div>
    <script>
      $(document).ready(function () {
        $("#submit").click(function (value) {
          // Using Regex expression for validating IPv4
          var ipaddress =
            /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
          var content = $("#ip").val();
  
          if (ipaddress.test(content)) {
            $("#demo").html("Ipaddress is Valid");
          } else {
            $("#demo").html("Ipaddress is invalid");
          }
        });
      });
    </script>
  </body>
</html>

Output: The following result is shown on entering a valid and invalid IP Address.

              

IP address validation output

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :