← Back to all snippets
JAVASCRIPT

Validate IPv4 Address Format

Learn how to use a regular expression to accurately validate the format of an IPv4 address, ensuring it adheres to standard dot-decimal notation for network inputs.

function isValidIPv4(ip) {
  return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip);
}
How it works: This JavaScript function employs a sophisticated regular expression to determine if a given string is a syntactically valid IPv4 address. The regex meticulously checks that each of the four octets is a number ranging from 0 to 255, correctly separated by dots, preventing malformed IP inputs.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs