aliencoders@mailing.commonworld is wrong
#########################
# Author: Sanjeev Kumar Jaiswal #
# Date: 31 March 2010 #
# Purpose: Email Validation #
##############################
#!C:/strawberry/perl/bin/perl.exe #change shebang according to your path settings
use strict;
use warnings;
# Taking Input from keyborad
my $email = <STDIN>;
# Removing the trailing new line
chomp($email);
# Pattern for Email validation
my $pattern= '^([a-zA-Z][\w\_\.]{6,15})\@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})$';
# To find the length of Username from the emailid (i.e. aliencoders@aliencoders.com The value before @ is username)
my @firstval=split('@',$email);
my $len=length($firstval[0]);
# Matching and displaying the result accordingly
if($len>15 || $len<6)
{
print "Invalid email id.\nLength of Username is $len which is either >15 or <6";
exit;
}
if($email=~m /$pattern/)
{
my $domain = $2;
if($domain=~ /^\-|\-$/)
{
print "Domain name can't start or end with -";
exit;
}
if($domain=~ /^\d/){
print "Domain Name can't start with Digit";
exit;
}
if(length($domain)>63 || length($domain) <2)
{
print "According to domain rule Domain length should lie between 3 and 63";
exit;
}
print "Its a valid Email ID";
}
else
{
print "invalid email format";
}
###### End of the code ############
I am checking proper email condition and then checking for email id length which should be between 6 and 15 (ex: jassics@aliencoders.com So, here my email id length is 7 <jassics>)
my $pattern= '^([a-zA-Z][\w\_]{6,15})\@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})$';
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (4)
Commented:
In particular <fred&barney@stonehenge.co
see
perldoc -q 'How do I check a valid mail address'
for the correct email address standard, and the correct way to do email address validation
Commented:
and by the way, there's no need for \ in front of _ or . in [\w\_\.]
Author
Commented:username could be anything but its good if it would start with a-z or A-Z for your comfort. Thats why I wrote like this.
There can many possible solutions for it.
but how may of users use exceptional email ids?
I was just trying to give an idea that how you can write regex in Perl for email validation and everyone is free to modify it according to their use.
I use it in one of our portal where restriction is above mentioned.
ex: gmail never allows special charatcer. so what do we say then?
RFC is different and customizing it according to our need i different bro.
Thanks for the comment by the way :D
Commented:
Here's the first one that I thought of:
Email::Valid - Check validity of Internet email addresses