Flickr widget

www.flickr.com
padamati's items Go to padamati's photostream

Thursday, December 4, 2008

IP address validation - form element

Validating an IP address, following script can be used.

#--- Checking IP format
if( $IP_Address =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})/ )
{
# valid format
if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
{
#valid range
print("IP address: $1.$2.$3.$4 --> All octets within range\n");
}
else
{
#invalid range
$error_message = "IP Address $IP_Address, One of the octets is out of range. All octets must contain a number between 0 and 255. ";
}
}
else
{
#invalid format
$error_message = "IP Address $IP_Address, Invalid format.";
}
if ( $error_message )
{
print( div( { style => "font-face: arial;color:red;font-size: larger;font-weight: " },"$error_message" ) );
exit 0;
}

return 1;
#---------------------


Thanks to http://snipplr.com/view/2801/verify-if-an-ipv4-ip-address-is-valid/

No comments: