How to check for IP address using regular expression in javascript? Last Updated : 23 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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;"> GeeksforGeeks </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? 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;"> GeeksforGeeks </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? Comment More infoAdvertise with us Next Article How to get client IP address using JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Validate Email Address without using Regular Expression in JavaScript ? Email validation in JavaScript is the process of ensuring that an email address entered by the user is in the correct format and is a valid email address or not. This is typically done on the client side using JavaScript before the form is submitted to the server.An email address must have the follo 5 min read JavaScript - How to Validate Form Using Regular Expression? To validate a form in JavaScript, you can use Regular Expressions (RegExp) to ensure that user input follows the correct format. In this article, we'll explore how to validate common form fields such as email, phone number, and password using RegExp patterns.1. Validating an Email AddressOne of the 4 min read JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using 3 min read How to get client IP address using JavaScript? Knowing a client's IP address can be useful for various purposes like personalizing content, tracking user activity, or offering location-based services. However, JavaScript running in the browser doesnât have direct access to this information due to security reasons. Using External APIs to Get the 3 min read Convert user input string into regular expression using JavaScript In this article, we will convert the user input string into a regular expression using JavaScript.To convert user input into a regular expression in JavaScript, you can use the RegExp constructor. The RegExp constructor takes a string as its argument and converts it into a regular expression object 2 min read How to Validate Email Address using RegExp in JavaScript? Validating an email address in JavaScript is essential for ensuring that users input a correctly formatted email. Regular expressions (RegExp) provide an effective and efficient way to check the email format.Why Validate Email Addresses?Validating email addresses is important for a number of reasons 3 min read Like