nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Email validation in Perl Code Sample

This is a discussion on Email validation in Perl Code Sample within the Coding in General forums, part of the Development/Scripting category; Dear All, Can anybody tell me how to validate email address in perl. Thanks,...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Linux answers from nixCraft.


Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-09-2008, 11:03 AM
Member
User
 
Join Date: Feb 2008
OS: fedora
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
chandanperl is on a distinguished road
Default Email validation in Perl Code Sample

Dear All,
Can anybody tell me how to validate email address in perl.

Thanks,
Reply With Quote
  #2 (permalink)  
Old 12-09-2008, 01:46 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
Default

You cannot do real-time validation of deliverable mail addresses. Most common pattern is as follows:
Code:
while $addr =~ s/\([^( )]*\)//g;
So...
Code:
if ($email_address =~ /^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$/) 
{ 
  print "$email_address is valid"; 
} 
else { 
  print "$email_address is invalid"; 
}
See post:
I Knew How To Validate An Email Address Until I Read The RFC
__________________
May the force with you!
Reply With Quote
  #3 (permalink)  
Old 12-09-2008, 03:00 PM
Member
User
 
Join Date: Feb 2008
OS: fedora
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
chandanperl is on a distinguished road
Default email validation in perl

Dear Monk,
It is not validating email id.

#!/usr/bin/perl
$email = "chndan.kumar@yahoo.co.in";

if ($email_address =~ /^(\w\-\_\.)+\@((\w\-\_)+\.)+[a-zA-Z]{2,}$/)
{
print "$email_address is valid";
}
else {
print "$email_address is invalid";
}
Reply With Quote
  #4 (permalink)  
Old 12-09-2008, 04:50 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by chandanperl View Post
Dear Monk,
It is not validating email id.

#!/usr/bin/perl
$email = "chndan.kumar@yahoo.co.in";

if ($email_address =~ /^(\w\-\_\.)+\@((\w\-\_)+\.)+[a-zA-Z]{2,}$/)
{
print "$email_address is valid";
}
else {
print "$email_address is invalid";
}
Replace:
Code:
$email = "chndan.kumar@yahoo.co.in";
With
Code:
$email_address  = "chndan.kumar@yahoo.co.in";
You should always use -w option:
Quote:
If something strange has gone wrong with your program and you’re not sure where you should look for help, try the -w switch first. It will often point out exactly where the trouble is.
]

Here is updated code for you:
Code:
#!/usr/bin/perl -w
$email_address = 'chndan.kumar@yahoo.co.in';

if ($email_address =~ /^(\w\-\_\.)+\@((\w\-\_)+\.)+[a-zA-Z]{2,}$/)
{
    print "$email_address is valid";
}
else {
    print "$email_address is invalid";
}
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help

Last edited by nixcraft; 12-09-2008 at 04:52 PM.
Reply With Quote
Reply

Tags
email validation , perl , perl email validation , regex , unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
convert perl code into binary executable chandanperl Coding in General 2 12-01-2010 01:03 PM
how to send email on every saturday in perl chandanperl Coding in General 4 22-08-2008 06:25 PM
RHCE Exam Sample Papers vikas027 CentOS / RHEL / Fedora 2 25-03-2008 05:51 PM
sample scripts for FTP in Unix jerry Shell scripting 1 23-06-2006 11:01 PM
RHCE sample questions require puppen Linux software 3 13-04-2006 06:36 PM


All times are GMT +5.5. The time now is 03:44 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38