How to check for IP address using regular expression in javascript?

The task is to validate the IP address both IPv4 as well as IPv6. Here we are going to use RegExp to solve the problem.
Approach 1:

  • RegExp: Which split the IP address on .(dot) and check for each element whether they are valid or not(0-255).

Example 1: This example using the appraoach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to check for IP address 
      using regular expression in javascript?
    </title>
</head>
<center>
  
    <body style="text-align:center;">
        <h1 style="color:green;">  
        GeeksForGeeks  
    </h1>
        <p id="GFG_UP" 
           style="font-size: 15px; 
                  font-weight: bold;">
        </p>
        <button onclick="GFG_Fun()">
            click here
        </button>
        <p id="GFG_DOWN"
           style="font-size: 24px;
                  font-weight: bold;
                  color: green;">
        </p>
        <script>
            var up = document.getElementById('GFG_UP');
            var down = document.getElementById('GFG_DOWN');
            var addr = '172.169.43.1';
            up.innerHTML =
              "Click on the button to validate the IP Address.<br>"
            + addr;
  
            function GFG_Fun() {
                down.innerHTML = 
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(addr);
            }
        </script>
    </body>
</center>
  
</html>

chevron_right


Output:



  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • RegExp: Which split the IP address on :(colon) and check for each element whether they are valid or not(0000-ffff).

Example 2: This example using the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to check for IP address 
      using regular expression in javascript?
    </title>
</head>
<center>
  
    <body style="text-align:center;">
        <h1 style="color:green;">  
        GeeksForGeeks  
    </h1>
        <p id="GFG_UP" 
           style="font-size: 15px; 
                  font-weight: bold;">
        </p>
        <button onclick="GFG_Fun()">
            click here
        </button>
        <p id="GFG_DOWN" 
           style="font-size: 24px;
                  font-weight: bold; 
                  color: green;">
        </p>
        <script>
            var up = document.getElementById('GFG_UP');
            var down = document.getElementById('GFG_DOWN');
            var addr = '2001:0db8:0000:0000:0000:ff00:0042:8329';
            up.innerHTML = 
              "Click on the button to validate the IP Address.<br>" 
            + addr;
  
            function GFG_Fun() {
                down.innerHTML = 
/^[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}\:[a-fA-F0-9]{1, 4}$/.test(addr);
            }
        </script>
    </body>
</center>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.