Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

Replies are listed 'Best First'.
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;


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


    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

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.
      * 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 *, which is a valid address, and pass on (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 (well formed, yes.. valid.. no). Checking the MX host will curb that.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58893]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2017-12-11 19:48 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (310 votes). Check out past polls.