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? Imagine your computer as your apartment. Just like each apartment has a unique address for receiving mail, your computer has an IP address that helps it receive information from the internet. This IP address acts like a label that identifies your specific device on the vast network of computers, ens 2 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 Ethereum Address Validation Using Regular Expressions ETHEREUM address is a 40-character hexadecimal identifier that receives and sends Ether (ETH) and other Ethereum-based tokens. Ethereum addresses come in two main formats: the standard hexadecimal representation and the checksummed format. Examples:Input: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e O 7 min read What is the valid range of a Class A network address? IPv4 addresses are categorized into five classes: A, B, C, D, and E. The order of bits in the first octet determines the class of an IP address. IPv4 address is divided into two parts:Network ID: Identifies the specific network to which a device belongs.Host ID: Identifies the specific device (host) 3 min read how to use validate_ipv4_address in django A validator is a callable that takes a value and raises a ValidationError if it doesnât meet the criteria. Validators can be useful for re-using validation logic between different types of fields. In this article, we will learn how to use the 'validate_ipv4_address' validator in Django. Required Mod 3 min read JavaScript - Validate URL in JS We will explore different approaches to validating URLs in JavaScript. These approaches include using regular expressions (regex), the URL constructor, and various npm packages. let's see one by one.All valid URLs follow a particular pattern. They have three main parts, which are: ProtocolDomain nam 2 min read How to Manipulate IP Addresses in Python using ipaddress Module? IP Address stands for internet protocol address. It's an identifying number that's related to a selected computer or network. When connected to the web, the IP address allows the computers to send and receive information. Python provides ipaddress module which provides the capabilities to create, ma 5 min read Like