Saturday, January 31, 2026
HomeLanguagesJavascriptHow to check for IP address using regular expression in javascript?

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

The task is to validate the IP address of 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 uses the approach discussed above. 

html




<h1 style="color:green;">
    neveropen
</h1>
<p id="GFG_UP"></p>
<button onclick="GFG_Fun()">
    click here
</button>
<p id="GFG_DOWN"></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>


Output: 

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

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

Approach 2:  

  • RegExp: 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. 

html




<h1 style="color:green;">
    neveropen
</h1>
<p id="GFG_UP"></p>
<button onclick="GFG_Fun()">
    click here
</button>
<p id="GFG_DOWN"></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>


Output: 

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

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS