Menu

Scripting Languages [ Lab Programs ]


validate IP address

Aim:

  b) Write a Perl script to validate IP address and email address.

Solution :

PROGRAM: (validate_ipaddr.pl)

 
#!/usr/bin/perl
use strict;
use warnings;

print "Enter an IP address: ";
chomp(my $ip = <STDIN>);

print "Enter an Email address: ";
chomp(my $email = <STDIN>);

# --------------------------
# Validate IP Address
# --------------------------
# Pattern checks 0–255 for each octet
my $ip_pattern = qr/^
    (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)  \.
    (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)  \.
    (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)  \.
    (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
$/x;

if ($ip =~ $ip_pattern) {
    print "Valid IP address\n";
} else {
    print "Invalid IP address\n";
}

# --------------------------
# Validate Email Address
# --------------------------
my $email_pattern = qr/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;

if ($email =~ $email_pattern) {
    print "Valid Email address\n";
} else {
    print "Invalid Email address\n";
}



OUTPUT:


# Sample Run 1:
Enter an IP address: 192.168.0.1
Enter an Email address: madhu@gmail.com
Valid IP address
Valid Email address

# Sample Run 2:
Enter an IP address: 158.23.1
Enter an Email address: abc@yahho.com
Invalid IP address
Valid Email address

# Sample Run 3:
Enter an IP address: 154.23.52.63
Enter an Email address: madhu@
Valid IP address
Invalid Email address




Related Content :

1. Write a Ruby script to create a new string which is n copies of a given string where n is a non-negative integer.   View Solution


2. Write a Ruby script which accept the radius of a circle from the user and compute the perimeter and area.    View Solution


3. Write a Ruby script which accept the users first and last name and print them in reverse order with a space between them.   View Solution


4. Write a Ruby script to accept a filename from the user print the extension of that.   View Solution


5. Write a Ruby script to find the greatest of three numbers.   View Solution


6. Write a Ruby script to print odd numbers from 10 to 1.   View Solution


7. Write a Ruby script to check two integers and return true if one of them is 20 otherwise return their sum.   View Solution


8. Write a Ruby script to check two temperatures and return true if one is less than 0 and the other is greater than 100.   View Solution


9. Write a Ruby script to print the elements of a given array.   View Solution


10. Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash.   View Solution


11. Write a TCL script to find the factorial of a number.   View Solution


12. Write a TCL script that multiplies the numbers from 1 to 10.   View Solution


13. Write a TCL script for sorting a list using a comparison function.   View Solution


14. Write a TCL script to
(i) create a list
(ii) append elements to the list
(iii) Traverse the list
(iv) Concatenate the list    View Solution


15. Write a TCL script to comparing the file modified times.   View Solution


16. Write a TCL script to Copy a file and translate to native format.   View Solution


17. a) Write a Perl script to find the largest number among three numbers.   View Solution
      b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.   View Solution


18. Write a Perl program to implement the following list of manipulating functions
a) Shift
b) Unshift
c) Push    View Solution


19. a) Write a Perl script to substitute a word, with another word in a string.   View Solution
      b) Write a Perl script to validate IP address and email address.   View Solution


20. Write a Perl script to print the file in reverse order using command line arguments.   View Solution