Return to Snippet

Revision: 3125
at November 12, 2010 11:04 by numberwhun


Updated Code
#!/usr/bin/perl

use strict;
use warnings;


print("What is the IP Address you would like to validate: ");
my $ipaddr = <STDIN>;
chomp($ipaddr);


if( $ipaddr =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/ )
{
    print("IP Address $ipaddr  -->  VALID FORMAT! \n");
    
    if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
    {
        print("IP address:  $1.$2.$3.$4  -->  All octets within range\n");
    }
    else
    {
        print("One of the octets is out of range.  All octets must contain a number between 0 and 255 \n");
    }
}
else
{
    print("IP Address $ipaddr  -->  NOT IN VALID FORMAT! \n");
}

Revision: 3124
at June 5, 2007 06:55 by numberwhun


Initial Code
#!/usr/bin/perl

use strict;
use warnings;


print("What is the IP Address you would like to validate: ");
my $ipaddr = <STDIN>;
chomp($ipaddr);


if( $ipaddr =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/ )
{
    print("IP Address $ipaddr  -->  VALID FORMAT! \n");
    
    if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
    {
        print("IP address:  $1.$2.$3.$4  -->  All octets within range\n");
    }
    else
    {
        print("One of the octets is out of range.  All octets must contain a number between 0 and 255 \n");
    }
}
else
{
    print("IP Address $ipaddr  -->  NOT IN VALID FORMAT! \n");
}

Initial URL


Initial Description
This code will validate not only the four octets contain between 1 and 3 numbers each, but also that the number they contain is between 0 and 255.  

In all, there are 3 different regex's that I tried and all seem to work fine.  The three regex's are:

1.  m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/  
2.  m/\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/
3.  m/\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}/

The first one is used below.  Enjoy!!!

Initial Title
Verify if an IPv4 IP address is valid

Initial Tags
validation, perl

Initial Language
Perl