Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

validate an email address ?

by Anonymous Monk
on Feb 16, 2001 at 21:08 UTC ( #58893=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to validate an emailaddress contained in $emailaddress.

I am trying to make sure that it contains at least @(text or numbers).(text or numbers)

I have been trying the following without success

$emailcheck=0; $_=$emailaddress; if ( !/\@([a-z][A-Z][0-9]\.+)([0-9][a-z][A-Z]+)/ ) { $emailcheck++; }

Does anyone have any ideas ?

Edit: 2001-03-03 by neshura

Comment on validate an email address ?
Download Code
Re: validate an email address ?
by arturo (Vicar) on Feb 16, 2001 at 21:15 UTC

    If you want decent validation, try Email::Valid.

    If you want your code to be readable, please wrap it in <code> tags.

    Now, to the immediate problem: is this cut and pasted? Your regex (as posted) is attempting to find things that *DON'T* match your pattern, i.e. $emailcheck will only be incremented if the address isn't in the format you've specified. While we're at it, here's the syntax for checking $emailaddress directly, with a little of Perl's syntactic neatness thrown in:

    $emailcheck++ if $emailaddress =~ /@[a-z0-9]+\.[a-z0-9]+/i;

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      there's also RFC822 if email valid doesn't work.

      And doesn't japhy's OGRE have a "real" 822 validating regexp for a test case? a regexp that's, oh, 500 lines long?

        The version of Email::Valid on my machine (0.12) uses the RFC822 pattern.
Re: validate an email address ?
by Gloom (Monk) on Feb 16, 2001 at 21:16 UTC
    if( $emailaddress =~ m!\@[.a-zA-Z0-9]+\.[a-zA-Z0-9]+$! ) { $emailcheck++ }

    But it's too simple to really check an email address..

    Update

    I only tried to answer simply to the question and adviced him to look for a more serious solution
    ___________________
    Hope this helps
      This is wrong, for all the normal reasons. {grin} Please search the wealth of info available on this topic already, or even the other messages likely to pop up on this thread alone!

      -- Randal L. Schwartz, Perl hacker

        Amen.
Re: validate an email address ?
by chipmunk (Parson) on Feb 16, 2001 at 21:32 UTC
Re: validate an email address ?
by batmonk (Scribe) on Feb 16, 2001 at 21:52 UTC
Re: validate an email address ?
by Pahrohfit (Sexton) on Feb 16, 2001 at 22:13 UTC
    I'm not sure why you only wish to validate the host/domain portion, and halfway at that (numbers in the .xxx ?), so here is a snippet to make sure the entire $emailaddress is valid.
    my $emailcheck = 0; if ($_ =~ /^[_a-z0-9.-]+\@[_a-z0-9.-]*\.\w\w(\w)?$/i){$emailcheck++;}
    I'm sure some one has already posted this question before, maby you should search before asking next time.
      *@foo.com is a valid address, which your test doesn't check. And _ is not valid in a domain name, which will pass your test. etc...

      I'm sure some one has already posted this question before, maby you should search before asking next time.

      The poster should have. And you should have made sure your regex would actually make sure the email address is valid. It will fail on *@foo.com, which is a valid address, and pass on foo@foo_bar.com (which has a valid format, but couldn't possibly be a valid address).

      Best advice is to use Email::Valid, with MX host checking. Without it, it will also miss-pass on things like foo@foo_bar.com (well formed, yes.. valid.. no). Checking the MX host will curb that.

      Cheers,
      KM

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58893]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2014-08-29 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls