email validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emirapoport
    New Member
    • Sep 2008
    • 4

    email validation

    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. tom.jones@email .com.. 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 )
    Code:
    # Do our error checking
      my $gintErrorCount = 0;
      my $gstrMessage = "";
      
      # Email Address
      unless ($gstrHash{email} =~ m/^(\w+)\@(\w+)\.(\w{2,4})$/)  {$gintErrorCount ++; $gstrMessage .= "Your email address is not properly formatted.<BR>";}
      
      # Fix phone variable - client
      $gstrHash{cphone} =~ s/\D//g;
      unless ($gstrHash{cphone} =~ m/^\d{10}$/) {$gintErrorCount ++; $gstrMessage .= "Your contact phone number must be a ten digit number.<BR>";}
      $gstrHash{cphone} =~ s/^([\d]{3}?)([\d]{3}?)([\d]{4}?)$/\($1\)$2-$3/i;   
     
      # Email Address
      unless ($gstrHash{cemail} =~ m/^(\w+)\@(\w+)\.(\w{2,4})$/)  {$gintErrorCount ++; $gstrMessage .= "Your contact email address is not properly formatted.<BR>";}
    i appreciate any help
    thanks
    EMI
    Last edited by numberwhun; Sep 8 '08, 01:42 AM. Reason: Please use code tags!
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    In that case, you could modify your regex to be something like:

    Code:
    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.

    Regards,

    Jeff

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      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.
      Code:
      my $email =~ /^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/;
      I still suggest Email::Valid.

      --Kevin

      Comment

      • emirapoport
        New Member
        • Sep 2008
        • 4

        #4
        thank you guys...i think that did it..just have to try a few tests live and see how it goes.
        much appreciated.
        Cheers
        Emi

        Comment

        • eWish
          Recognized Expert Contributor
          • Jul 2007
          • 973

          #5
          What did you end up using?

          --Kevin

          Comment

          Working...