Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript - Check if a String is a Valid IP Address Format
Next article icon

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 Expressions

This 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)); 

Output
true

Using Split and Validate

This 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)); 

Output
true

Using Library Functions

There 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 library

npm 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));  

Output

true

Using '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)); 

Output
true 

Next Article
JavaScript - Check if a String is a Valid IP Address Format

A

anjugaeu01
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • javascript-string
  • Geeks Premier League 2023

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences