Validating an Email Address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rag84dec
    New Member
    • Mar 2007
    • 100

    Validating an Email Address

    Hi
    can anyone help me in writing this

    here is the code i wrote which is not woring properly


    Code:
    print "Enter the E-Mail address\n";
    $_=<>;
    
    if($_[0] !~ m\[0-9]\)
    {
    	if(/[a-zA-Z[\.]_]+@[a-zA-Z]+.[a-zA-Z]\.[a-zA-Z]/)
    	{
    		print "its a valid Address\n";
    	}
    	else
    	{
    		print "Sorry,Its not a valid address\n";
    	}
    }
    else
    {
    	print "Sorry,Its not a valid address\n";
    }
    thanks
    Last edited by miller; Apr 2 '07, 10:00 AM. Reason: Code tag
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    this looks wrong:

    Code:
    [a-zA-Z[\.]_]

    maybe you meant:

    Code:
    [a-zA-Z._]

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Hi rag84dec,

      As Kevin pointed out, your code is full of syntax errors. Quickly editting out the obvious ones gives me this:

      Code:
      print "Enter the E-Mail address\n";
      $_ = <>;
      chomp;
       
      if(m/\d/) {
      	print "Sorry,Its not a valid address\n";
      } elsif(/[a-zA-Z._]+\@[a-zA-Z]+.[a-zA-Z]\.[a-zA-Z]/) {
      	print "its a valid Address\n";
      } else {
      	print "Sorry,Its not a valid address\n";
      }
      However, that only touches on your problems, as your logic and regex's appear to be flawed.

      I would advise you and anyone to use one of the many Email Validating modules on cpan instead of creating your own logic. Here's one of the first ones that came up in my search, and I'm certain that it will fit your purposes:

      cpan Email::Valid

      - Miller

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Ditto Miller. ;)

        Comment

        Working...