Regex to validate an IP address: IPv4 and IPv6 patterns

Regex to validate an IP address: IPv4 and IPv6 patterns

The snippet below is a regular expression that validates IP addresses. It works the same in JavaScript, in PHP and in HTML, and I have added a few handy functions built on top of it to check whether an IP address has a valid format.

IPv4 regular expression

php
(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}

IPv6 regular expression

php
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))

JavaScript version

The format depends on the version: an IPv4 address looks like 0.0.0.0.

An IPv6 address looks like 2001:0db8:3c4d:0015:0000:d234::3eee:0000

The function takes an IP address as a string and compares it against the regular expression. If the string does not match the format, the function returns false, if it does, the function returns true.

ip address regex javascript

javascript
/**
 * Validate an IP with a regular expression
 *
 * Returns false if the IP is not valid, otherwise IPv4 or IPv6
 *
 * @param ip
 * @return Boolean
 */
function validateIP(ip){

if(isIPv4(ip)) {
        return 'IPv4';
    } elseif (isIPv6(ip)) {
        return 'IPv6';
    }

return false;
}

/**
 * Validate an IPv4 with a regular expression
 *
 * Returns false if the IP is not valid
 *
 * @param ip
 * @return Boolean
 */
function isIPv4(ip){
    var Reg = new RegExp(/^(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/i);
    return Reg.test(ip);
}

/**
 * Validate an IPv6 with a regular expression
 *
 * Returns false if the IP is not valid
 *
 * @param ip
 * @return Boolean
 */
function isIPv6(ip){
    var Reg = new RegExp(/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/i);
    return Reg.test(ip);
}

Using the regex in JavaScript

The simplest way to use the regular expression is inside an IF condition, as below.

javascript
let ip;
if(ip = validateIP('192.168.0.1')){
  alert(ip + "valide");
} else {
  alert("Adresse IP Invalide");
}

if(ip = validateIP('2001:0db8:3c4d:0015:0000:d234::3eee:0000')){
  alert(ip + "valide");
} else {
  alert("Adresse IP Invalide");
}

See the Pen
Valider une adresse IP avec une expression régulière
by Damien Flandrin (@dam62500)
on CodePen.0

PHP version

To read an IP address in PHP, rely on$_SERVER[‘REMOTE_ADDR’] , which holds the real address of the party connecting. It is the most reliable value you will get.

That party may sit behind a proxy, though, in which case the proxy may have set $_SERVER[‘HTTP_X_FORWARDED_FOR’], a value that is trivial to spoof. It can be set by someone with no proxy at all, or the address it carries may be an internal LAN address behind the proxy.

So if you log $_SERVER[‘HTTP_X_FORWARDED_FOR’], make sure you log $_SERVER[‘REMOTE_ADDR’] as well, for instance in two separate columns of your database.

And if you store an IP address as a string, allow at least 45 characters. IPv6 is here to stay, and its addresses are longer than the old IPv4 ones.

Careful

Think twice before storing an IP address in a database: your application has to stay on the right side of the GDPR.

ip address regex php

php
/**
 * Validate an IP with a regular expression
 *
 * Returns false if the IP is not valid, otherwise IPv4 or IPv6
 *
 * @param $ip
 * @return Boolean
 */
function validateIP($ip){

if(isIPv4($ip)) {
        return 'IPv4';
    } elseif (isIPv6($ip)) {
        return 'IPv6';
    }

return false;
}

/*
 * Validates IPv4
 * @param $ip
 */
 function isIPv4($ip)
 {
    return preg_match('/^(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/i', $ip);
}

/*
 * Validates IPv6
 * @param $ip
 */
 function isIPv6($ip)
 {
    return preg_match('/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/i', $ip);
 }
php
if ($ip = validateIP($_SERVER['REMOTE_ADDR']) {
    echo "Mon IP est une " . $ip;
    // My IP is an IPv4
} else {
    echo "IP Invalide !";
}

HTML version

You can also validate a field directly in the markup with the pattern attribute of <input>. Bear in mind that this is only there to show the user something is wrong, with a red border for instance, it is not validation you can trust.

ip address regex html

html
<input
      name="ip"
      type="text"
      pattern="(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}"
      placeholder="#.#.#.#"
     />

See the Pen
Untitled
by Damien Flandrin (@dam62500)
on CodePen.0

HTMLJavaScriptPHP

Damien Flandrin Web developer since 2010, creator of Gekkode and Email Impact. Every article is tested on a real project before publication. Contact
Newsletter

New tests, tutorials and projects, by e-mail.

Reproducible tests, versioned code, dated results. Never any spam.