Gossamer Forum
Home : General : Perl Programming :

Newbie - brain on fire!

(Page 1 of 2)
> >
Quote Reply
Newbie - brain on fire!
Hi guys,
I am new to programming and perl. Am learning how to create feedback forms. I seem to get stuck at email validation. What am I doing wrong? Can someone please help me.Unsure

Thanks a lot. Here is how my script reads

#Validate input of correct email address

$good_email = 0;

@check_email = split (//, $email);
foreach $letter (@check_email)

{
if ($letter eq "@")
{
$good_email = 1;
last;
}
}


print("Content-Type: text/html\n\n");
print "<HTML>\n<HEAD>\n";



if (!$good_email)
{
print "<TITLE>Invalid email address</TITLE>\n\<BODY>\n";
print "<H2>Invalid Email Address detected</H2>";
print "<p>Please provide correct email address. Go back to the form</p>\n";
}
else
{
print "<TITLE>Thank You</TITLE>\n<BODY>\n";
print "<P>Thank you for filling out the form. We will contact you soon at $email</P>";
}
Quote Reply
Re: [shaan] Newbie - brain on fire! In reply to
If you look down about 10 threads you'll see:

reg ex to validate email address
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
or.. you can stay right here.. and ignore paul.

remove this..

$good_email = 0;

@check_email = split (//, $email);
foreach $letter (@check_email)

{
if ($letter eq "@")
{
$good_email = 1;
last;
}
}

replace

if (!$good_email) {

with

if ($email !~ /^[\w_-\.+]+@[\w_-\.]\.\w+$/) {

not sure if those are all the characters that can go in an email.. i think it is.. oh well.
Quote Reply
Re: [widgetz] Newbie - brain on fire! In reply to
Revised script...


@check_email = split (//, $email);
foreach $letter (@check_email)
{
if ($letter eq "@")
{
$good_email =1;
last;
}
}


print("Content-Type: text/html\n\n");
print "<HTML>\n<HEAD>\n";

if ($email !~ /^[\w_-\.+]+@[\w_-\.]\.\w+$/) {
{
print "<TITLE>Invalid email address</TITLE>\n\<BODY>\n";
print "<H2>Invalid Email Address detected";
print "<p>Please provide correct email address</p>\n";
}
else
{
print "<TITLE>Thank You</TITLE>\n<BODY>\n";



QUESTION
1. Don't I have to assign an initial value to $good_email?

2. Can I use "==" instead of "eq" in if ($letter..)?
Quote Reply
Re: [shaan] Newbie - brain on fire! In reply to
You should be aware that Jerry's code will match:

______________@______________.__________

.....................@..........................._

-----------@____________._

...and quite a few other combo's. If you want something reliable you should use the Email::Valid perl module.

http://search.cpan.org/search?dist=Email-Valid

Jerry's code is also longer than it needs to be. He has used a character class ( [ ] ) including \w and _ but \w already includes underscores so the _ is redundant.

>>
QUESTION
1. Don't I have to assign an initial value to $good_email?

2. Can I use "==" instead of "eq" in if ($letter..)?
<<

You don't need to know the answer to those questions as you shouldn't be using that code :)

Last edited by:

Paul: Jul 30, 2002, 7:54 AM
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
Thanks so much guys (P$W)!
It works!!

Thanks for your help.
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
oh wow. Shocked you're right. lol.

it is possible to have periods in yoru emails.. heres another.

/^[^a-z0-9]+([_-\.]{1}[^a-z0-9]+)*?\@[^a-z0-9]+([_-\.]{1}[^a-z0-9]+)*?\.[a-z0-9]{2,3}$/i

oof. correct that if you see anything wrong with it. it's quite long, but i think it does the job for most situations.

--
jsu
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
/^[^a-z0-9]+([_-\.]{1}[^a-z0-9]+)*?\@[^a-z0-9]+([_-\.]{1}[^a-z0-9]+)*?\.[a-z0-9]{2,3}$/i


Hmm you don't need to escape @'s but more importantly it is poorly formatted (you need to escape - inside [ ]) and won't match basic emails like pwilson@aol.com as you are blocking a-z and 0-9
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
I would give up trying to come up with a regex to match an email, it's a waste of effort. Even the four page regex posted earlier is not going to match some emails that are deliverable (it only matches rfc compliant emails, however many emails that are not rfc compliant can still get delivered). If your goal is to test that the email is valid, then you need to send a validation email to the user and have them click on a validation link. If your goal is to catch typos, then \S@\S+\.\S+ is fine.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
that's what i get for doing things in the early morning..

isn't it clear that you don't block the a-z0-9.. thought you'd catch that. also.. i tend to backslash anything that is used for variables in perl. @ $ % { }.. it's just a thing i do. does the compiler care?

/^[a-z0-9]+([_-\.]{1}[a-z0-9]+)*\@[a-z0-9]+([_-\.]{1}[a-z0-9]+)*\.[a-z0-9]{2,3}$/i

--
jsu

Last edited by:

Seto Kaiba: Jul 30, 2002, 1:29 PM
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
>>
does the compiler care?
<<

Yes, well it does regarding -

Last edited by:

Paul: Jul 30, 2002, 1:31 PM
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
/^[a-z0-9]+([_\-\.][a-z0-9]+)*\@[a-z0-9]+([_\-\.][a-z0-9]+)*\.[a-z0-9]{2,3}$/i


So we agree this works now?

--
jsu

Last edited by:

Seto Kaiba: Jul 30, 2002, 2:01 PM
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
Well I agree it compiles, but it isn't very good :)

Try running this...

Code:
#!/perl/bin/perl
#==========================================================

use strict;

main();

#==========================================================

sub main {
#----------------------------------------------------------
# Oops!

print "Content-type: text/plain\n\n";
foreach (qw/pwilson@wiredon.net
pwilson@aol.com
pwilson@aol.co.uk
test@123-domain.com
test@123-domain.co.uk
foo@bar.info
me@here.info
foo@123-domain.ws
i_m@valid.com
_imvalid@here.com/) {
print /^[a-z0-9]+([_\-\.][a-z0-9]+)*\@[a-z0-9]+([_\-\.][a-z0-9]+)*\.[a-z0-9]{2,3}$/i ? "$_ is OK\n" : "$_ is NOT OK\n";
}
}

Last edited by:

Paul: Jul 30, 2002, 4:38 PM
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
_imvalid@here.com is valid??

err.. if so.. then just add _ into the first bracket.. but i'm not even allowed to create _email@jsu07.com on my server

--
jsu
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
>>
but i'm not even allowed to create _email@jsu07.com on my server
<<

I can, I didn't bother to check if it was valid or not but anway the regex still won't match info domains.

>>
err.. if so.. then just add _ into the first bracket..
<<

I wouldn't ;)


Like Alex said, i'd give up.

Last edited by:

Paul: Jul 30, 2002, 4:47 PM
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
didn't think of 4 letter tlds.

/^_?[a-z0-9]+([_\-\.][a-z0-9]+)*\@[a-z0-9]+([_\-\.][a-z0-9]+)*\.[a-z0-9]{2,4}$/i

if you want to be critical.

"give up"..

never. that's something i've never done. and i think using 1 line surely beats a whole module or library.

--
jsu
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
Like Alex mentioned without writing something huge you won't even get close to writing something decent.

I'd prefer to use \S+@\S+\.\S+ rather than using yours which has the potential to reject many valid addresses.
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
eh. I've just looked at some asp source codes in the msdn.

/\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/

is what they use. however, i can see where that may go wrong.

the problem with using \S+@\S+\.\S+ is you can get.. @@@.@ and many other combonations of non-whitespace emails.

at least make a check for a single @ and filter out the other invalid characters.

--
jsu
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
>>
the problem with using \S+@\S+\.\S+ is you can get.. @@@.@ and many other combonations of non-whitespace emails.

at least make a check for a single @ and filter out the other invalid characters.
<<

I'd prefer it to accept silly emails than reject valid ones.
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
my point is.. rather than using \S+ use something else..

[\w\.]+@[\w\.]+\.[A-Za-z]+

will even work better. just figure out what all those valid characters are.. i think \w\. are the only ones. \w matches _ and -, so A-Z a-z 0-9 _ - and . are all there.

--
jsu
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
You aren't grasping this are you :) ...as soon as you start trying to get accurate you'll end up blocking valid emails, you either need to be very general or very precise with your regex.

Do you really think Email::Valid (on CPAN) would be about 50 lines long if it was as simple as you are trying to make out.
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
Code:
/^[^\s<>@"\:]+@([\w\-]+\.)*([\w\-]+)\.[a-z]{2,5}$/i && $2 !~ /(__|--)/


prove me wrong. please. i tested it on every email address possible and impossible.. i turned on my wildcards and made myself a new email address.

~`!#$%^&*-_+={}[]|;,.,'?/@jsu07.com

it works nicely. ------------.jsu07.com can be used too.. it's not a one-liner.. but it WILL validate email addresses.

think about it.. email address validation can't be too hard. if its possible to validate a domain name.. then you've already got 2/3s of the entire thing. the username can consist of almost anything.. and with that code, i look for the invalids.

i derived it from putting together a longer subroutine originally.. and went through it to shorten the entire thing. the original subroutine i made checked everything 1by1 split up the email address and then i just condensed it (hence all the edits)

--
jsu

Last edited by:

Seto Kaiba: Jul 30, 2002, 7:38 PM
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
my bad. missed ; and ,

anyways..

/^[^\s<>@"\:;,]+@([\w\-]+\.)*([a-z0-9\-]+)\.[a-z]{2,5}$/i

changed a bit.. because internic says you can't register names with an underscore and also.. no punctuations except for a -..

also...

#!perl
use strict;
use Email::Valid;

# ~`!#$%^&*-_+={}[]|.'?/@jsu07.com

my $email = qq~\~`!#\$%^&*-_+={}[]|.'?/\@jsu07.com~;
print $email . " " . (Email::Valid->address ($email) ? "is valid.\n" : "is not valid.\n");


and finally.. the output..

~`!#$%^&*-_+={}[]|.'?/@jsu07.com is not valid.

of course.. in my inbox..

Envelope-to: "~`!#$%^&*-_+={}[]|.'?/"@jsu07.com
X-Sender: email@mail.jsu07.com
X-Mailer: QUALCOMM Windows Eudora Version 5.1.1
Date: Tue, 30 Jul 2002 20:22:22 -0700
To: ~`!#$%^&*-_+={}[]|.'?/@jsu07.com
From: Jerry Su <email@jsu07.com>
Subject: Test

test


point is.. i'm just as right as Email::Valid.

--
jsu

Last edited by:

Seto Kaiba: Jul 30, 2002, 8:35 PM
Quote Reply
Re: [Seto Kaiba] Newbie - brain on fire! In reply to
>>
~`!#$%^&*-_+={}[]|.'?/@jsu07.com is not valid.

of course.. in my inbox..

Envelope-to: "~`!#$%^&*-_+={}[]|.'?/"@jsu07.com
<<

Email::Valid is right...those characters aren't valid..they may send, but aren't valid.

Last edited by:

Paul: Jul 31, 2002, 3:47 AM
Quote Reply
Re: [Paul] Newbie - brain on fire! In reply to
you got to ignore those quotes.. eudora adds those in.

also.. email::valid will say valid to domains with exclamation marks and underscores in them. when have you ever seen one of those.

the email address is valid.. try sending an email to it. won't bounce.

--
jsu

Last edited by:

Seto Kaiba: Jul 31, 2002, 8:05 AM
> >