JavaScript - Check if a String is a Valid IP Address Format Last Updated : 03 May, 2025 Comments Improve Suggest changes Like Article Like Report An IP address is a unique identifier assigned to each device connected to a computer network that uses the Internet Protocol for communication. There are two common types of IP addresses: IPv4 and IPv6. In this article, we’ll explore how to check if a string is a valid IP address format in JavaScript.Using Regular ExpressionsThis approach uses regular expressions to match valid IPv4 and IPv6 patterns. JavaScript function checkIp(ip) { const ipv4 = /^(\d{1,3}\.){3}\d{1,3}$/; const ipv6 = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/; return ipv4.test(ip) || ipv6.test(ip); } const ipAddress = "122.0.0.0"; console.log(checkIp(ipAddress)); OutputtrueUsing Split and ValidateThis approach splits the string by periods or colons and validates each part individually. JavaScript function validIp(ip) { const parts = ip.split(/[.:]/); if (parts.length === 4) { // Check IPv4 parts for (const part of parts) { const num = parseInt(part); if (isNaN(num) || num < 0 || num > 255) { return false; } } return true; } else if (parts.length === 8) { // Check IPv6 parts for (const part of parts) { if (!/^[0-9a-fA-F]{1,4}$/.test(part)) { return false; } } return true; } return false; } const ipAddress = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; console.log(validIp(ipAddress)); OutputtrueUsing Library FunctionsThere are some libraries available in JavaScript that make IP address validation easier. One popular library is ip-address. This library helps to easily check whether an IP address is valid.First, install the librarynpm install ip-address JavaScript const ip = require('ip-address'); function checkIp(ipAddress) { try { const parsed = new ip.Address6(ipAddress); return parsed.isValid() || new ip.Address4(ipAddress).isValid(); } catch (e) { return false; } } const ipAddress = "192.168.1.1"; console.log(checkIp(ipAddress)); OutputtrueUsing 'net' module (Node.js specific)If you are using Node.js, you can use the built-in net module to check if an IP address is valid. The net module provides functions like is IPv4() and is IPv6() to check if the address is valid. JavaScript const net = require('net'); function isValidIp(ipAddress) { // For IPv4 if (net.isIPv4(ipAddress)) { return true; } // For IPv6 if (net.isIPv6(ipAddress)) { return true; } return false; } const ipAddress = "192.168.1.1"; console.log(isValidIp(ipAddress)); Outputtrue Comment More infoAdvertise with us Next Article JavaScript - Check if a String is a Valid IP Address Format A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-string Geeks Premier League 2023 +1 More Similar Reads 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 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 abo 2 min read Perl | Extract IP Address from a String using Regex Perl stands for Practical Extraction and Reporting Language and this not authorized acronym. One of the most powerful features of the Perl programming language is Regular Expression and in this article, you will learn how to extract an IP address from a string. A regular expression can be either sim 4 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 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 Like