Tuesday, 7 March 2023

Perl Code for Gmail Validator



To validate a Gmail address using regular expressions in Perl, you can use the following code:


#!/usr/bin/perl

use strict;

use warnings;

sub is_valid_gmail {

    my $email = shift;

    # Regular expression pattern for Gmail address validation

    my $pattern = qr/^[a-zA-Z0-9._%+-]+@gmail\.com$/;

    if ($email =~ $pattern) {

        return 1;  # Valid Gmail address

    } else {

        return 0;  # Invalid Gmail address

    }

}

# Testing the function

my $email1 = 'example@gmail.com';

my $email2 = 'invalid_email@gmail.com';


if (is_valid_gmail($email1)) {

    print "$email1 is a valid Gmail address\n";

} else {

    print "$email1 is not a valid Gmail address\n";

}


if (is_valid_gmail($email2)) {

    print "$email2 is a valid Gmail address\n";

} else {

    print "$email2 is not a valid Gmail address\n";

}


This code defines a Perl subroutine is_valid_gmail that takes an email address as input and checks if it is a valid Gmail address. The regular expression pattern ^[a-zA-Z0-9._%+-]+@gmail\.com$ is used to match the email address against the Gmail format.

To test the function, two email addresses are provided: example@gmail.com and invalid_email@gmail.com. The function is called for each email address, and the result is printed to the console.

Please note that this code only validates the format of the email address and checks if it ends with @gmail.com. It does not verify if the email address actually exists or is currently in use.

Labels: