miken32/network-rules

Laravel validation rules for checking IP addresses and networks
92,677 14
Install
composer require miken32/network-rules
Latest Version:v2.2
PHP:^8.1
License:AGPL-3.0
Last Updated:Feb 18, 2026
Links: GitHub  ·  Packagist
Maintainer: miken32

Installation

composer require miken32/network-rules

Available Validation Rules

Here is a list of the available rules and their usage.

In Network Outside Network IP Or Net Network Netv4 Netv6 Private IP Private IPv4 Private IPv6 Private Net Routable IP or Net Routable IP Routable IPv4 Routable IPv6 Routable Net Routable Netv4 Routable Netv6

in_network:cidr,...

The field under validation must be an IP address within one of the given networks. The networks must be given in CIDR notation, and may be either IPv4 or IPv6 networks.

'ip4_address' => 'in_network:192.168.0.1/24',
'some_address' => 'in_network:192.168.0.0/24,192.168.1.0/24,192.168.2.0/24',
'ip6_address' => 'in_network:fd03:224f:a5c3:99ae::0/64'

outside_network:cidr,...

The field under validation must be an IP address outside all of the given networks. The networks must be given in CIDR notation, and may be either IPv4 or IPv6 networks.

'ip4_address' => 'outside_network:192.168.0.1/24',
'some_address' => 'outside_network:192.168.0.0/24,192.168.1.0/24,192.168.2.0/24',
'ip6_address' => 'outside_network:fd03:224f:a5c3:99ae::0/64'

ip_or_net

The field under validation must be an IP address or network in CIDR notation. The address or network may be either IPv4 or IPv6.

network

The field under validation must be an IP network in CIDR notation. The network may be either IPv4 or IPv6; use individual rules to limit mask bits.

netv4:low,high

The field under validation must be an IPv4 network in CIDR notation. If provided, the number of bits in the mask must be between low and high.

'bounded_network'   => 'netv4:20,24',
'unbounded_network' => 'netv4'

netv6:low,high

The field under validation must be an IPv6 network in CIDR notation. If provided, the number of bits in the mask must be between low and high.

'bounded_network'   => 'netv6:56,64'
'unbounded_network' => 'netv6'

private_ip

The field under validation must be a private IPv4 or IPv6 address. Private addresses are defined as being within one of the following networks:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16
  • fc00::/7

The following networks are considered reserved, not private. They are not considered valid by this rule:

  • 0.0.0.0/8
  • 127.0.0.0/8
  • 169.254.0.0/16
  • 240.0.0.0/4
  • ::/128
  • ::1/128
  • 2001:10::/28
  • 2001:db8::/32
  • 3ffe::/16
  • 5f00::/8
  • fe80::/10

private_ipv4

The field under validation must be a private IPv4 addresses. The networks considered private are described in the private_ip rule.

private_ipv6

The field under validation must be a private IPv6 addresses. The networks considered private are described in the private_ip rule.

private_net

The field under validation must be a private IP network in CIDR notation. The networks considered private are described in the private_ip rule.

routable_ip_or_net

The field under validation must be a globally routable IP address or network in CIDR notation. This excludes all private and reserved ranges, as detailed the the private_ip rule.

routable_ip

The field under validation must be a globally routable IPv4 or IPv6 address. This excludes all private and reserved ranges, as detailed the the private_ip rule.

routable_ipv4

The field under validation must be a globally routable IPv4 address. This excludes all private and reserved ranges, as detailed the the private_ip rule.

routable_ipv6

The field under validation must be a globally routable IPv6 address. This excludes all private and reserved ranges, as detailed the the private_ip rule.

routable_net:low,high

The field under validation must be a globally routable IP network in CIDR notation. The network must not overlap any private or reserved ranges, as detailed the the private_ip rule. If provided, the number of bits in the mask must be between low and high.

routable_netv4:low,high

The field under validation must be a globally routable IPv4 network in CIDR notation. The network must not overlap any private or reserved ranges, as detailed the the private_ip rule. If provided, the number of bits in the mask must be between low and high.

routable_netv6:low,high

The field under validation must be a globally routable IPv6 network in CIDR notation. The network must not overlap any private or reserved ranges, as detailed the the private_ip rule. If provided, the number of bits in the mask must be between low and high.

Usage

The included validation rules can be used either as traditional string-based validation rules or as instantiated classes. The following code blocks perform identical validations.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MyFormRequest extends FormRequest
{
    public function rules(): array
    {
        return [

          'address'      => ['in_network:192.168.10.0/24'], // must be an IPv4 address in the specified network
          'subnet'       => ['netv4'], // must be an IPv4 CIDR network
          'ipv6_subnet'  => ['netv6:48,56'], // must be an IPv6 CIDR network between 48 and 56 bits
        ];
    }
}
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Miken32\Validation\Network\Rules;

class AnotherFormRequest extends FormRequest
{
    public function rules(): array
    {
        return [
          'address'      => [new Rules\InNetwork('192.168.10.0/24')], // must be an IPv4 address in the specified network
          'subnet'       => [new Rules\Netv4()], // must be an IPv4 CIDR network
          'ipv6_subnet'  => [new Rules\Netv6(48, 56)], // must be an IPv6 CIDR network between 48 and 56 bits
        ];
    }
}

Note, for all methods that accept a minimum or maximum bit mask, unspecified arguments default to the minimum or maximum count, depending on the type of network. For example, new Rules\Netv6(48) or netv6:48 are equivalent to new Rules\Netv6(48,128) or netv6:48,128. To specify an exact bit mask, use the same value for both arguments: new Rules\Netv6(48, 48) or netv6:48,48.

Standalone Usage

These checks can be used outside the Laravel framework as well; the Util class provides many static methods for validating input using the framework (or not!) of your choice.

<?php

use Miken32\Validation\Network\Util;

$allowed_network = '172.16.40.0/22';

if (!Util::addressWithinNetwork($_POST["ip_address"], $allowed_network)) {
    // failure!
}

if (Util::validPrivateIPAddress($_POST["private_ip"])) {
    // success!
}

if (Util::validIPv6Network($_POST["ipv6_network"]), 56) {
    // success!
}

Related Packages

propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

43,538,120 3,026
prettus/laravel-validation

Laravel Validation Service

11,862,350 406
pixelpeter/laravel5-isocodes-validation

Laravel 5 wrapper for the IsoCodes Validation library from ronanguilloux

87,867 19
skysplit/laravel5-intl-translation

Laravel 5 package for better translation syntax using php-intl extension

106,815 10
proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rul...

2,445,464 1,140