By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

email validation

P: 4
Hello, im posting this question because i have zero knowledge on perl. I have a script which curently allows users to input an email address and if the email is not valid it wont allow the submission. Everything works fine except that now there are clients who have a period in their email address and it is rejected. eg. im sure its a simple addition to the script not even sure where it might go but here is what i copied and where im assuming it might need to be changed..

Code: ( text )
Expand|Select|Wrap|Line Numbers
  1. # Do our error checking
  2.   my $gintErrorCount = 0;
  3.   my $gstrMessage = "";
  5.   # Email Address
  6.   unless ($gstrHash{email} =~ m/^(\w+)\@(\w+)\.(\w{2,4})$/)  {$gintErrorCount ++; $gstrMessage .= "Your email address is not properly formatted.<BR>";}
  8.   # Fix phone variable - client
  9.   $gstrHash{cphone} =~ s/\D//g;
  10.   unless ($gstrHash{cphone} =~ m/^\d{10}$/) {$gintErrorCount ++; $gstrMessage .= "Your contact phone number must be a ten digit number.<BR>";}
  11.   $gstrHash{cphone} =~ s/^([\d]{3}?)([\d]{3}?)([\d]{4}?)$/\($1\)$2-$3/i;   
  13.   # Email Address
  14.   unless ($gstrHash{cemail} =~ m/^(\w+)\@(\w+)\.(\w{2,4})$/)  {$gintErrorCount ++; $gstrMessage .= "Your contact email address is not properly formatted.<BR>";}
i appreciate any help
Sep 8 '08 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 2.5K+
P: 3,503
In that case, you could modify your regex to be something like:

Expand|Select|Wrap|Line Numbers
  1. m/^(\w+\.*\w*)\@(\w+)\.(\w{2,4})$/
Mind you, that is totally untested, but in theory, I believe it should work. Test it to find out. The \.*\w* will only match if they are present.


Sep 8 '08 #2

Expert 100+
P: 971
I would suggest that you use a trusted and proven module called Email::Valid. Is it perfect? No, however it is going to be better than designing your own. Check out the source code and see the regex the author built.

If you want to do you own you could use something like this.
Expand|Select|Wrap|Line Numbers
  1. my $email =~ /^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/;
I still suggest Email::Valid.

Sep 8 '08 #3

P: 4
thank you guys...i think that did it..just have to try a few tests live and see how it goes.
much appreciated.
Sep 10 '08 #4

Expert 100+
P: 971
What did you end up using?

Sep 10 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.