Re: Validate newline delimited email list
by japhy (Canon) on Jan 12, 2007 at 21:08 UTC
|
First of all, use split /\n/. Don't put a literal newline in your code -- that's ugly.
Second, the chomping is entirely unnecessary if you're splitting the string on newlines. Third, are you entirely sure the email addresses contain validly formatted addresses and nothing else? I'd suggest doing:
foreach (@other_addresses) {
my $ok = Email::Valid->address($_);
print "address($_) = $ok\n";
}
| [reply] [d/l] [select] |
Re: Validate newline delimited email list
by ikegami (Patriarch) on Jan 12, 2007 at 21:14 UTC
|
Works for me except I had to initialize $email_list, initialize $error_message and print $error_message. Could you provide a snippet that actually shows the error?
There are a couple of details worth mentioning.
-
split( /\n/, ... )
is easier to read than
split( /
/, ... )
-
... if( $error > 0 );
or just
... if $error;
is easier to read than
... unless( $error < 1 );
| [reply] [d/l] [select] |
|
the current version of this code looks like
my ($error) = 0;
my (@other_addresses) = split( /\n/, $email_list );
foreach (@other_addresses) {
my $ok = Email::Valid->address($_);
$error++ if(!$ok);
print "address($_) = $ok\n";
print "error ".$error."<br>";
}
$error_message .= "$error Email list contains an invalid email ad
+dress.<br>" if $error;
and gives me
address(test@test.com ) = error 1
address(test2@test.com) = test2@test.com error 1
There was an error with your form submission.
1 Email list contains an invalid email address.
it seems to let every address through whether it is valid or not. | [reply] [d/l] [select] |
|
sub trim {
my ($s) = @_;
for ($s) {
s/^\s+//;
s/\s+$//;
return $_;
}
}
...
my @other_addresses = map trim, split( /\n/, $email_list );
...
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
Looks to me that this line
$error++ if(!$ok);
is merely testing whether $ok exists, which it always does according to the line above it.
my $ok = Email::Valid->address($_);
Try testing for the value of $ok that was returned instead.
if ($ok == 1) {#the rest of your code
} else {# code here too if you like }
| [reply] [d/l] [select] |
Re: Validate newline delimited email list
by ikegami (Patriarch) on Jan 12, 2007 at 20:48 UTC
|
Something's wrong with what you posted. Some lines are missing (where is $error set?) and the print line is cut halfway through.
| [reply] [d/l] [select] |
|
$error is set in the first line of code. I've revised the print line. Thanks for taking a look...
| [reply] |
|
$error++ unless(Email::Valid->address($_));
| [reply] [d/l] [select] |
Re: Validate newline delimited email list
by MonkE (Hermit) on Jan 12, 2007 at 21:53 UTC
|
Are you using Perl 5.0.0.4 or newer? Because it's a preqrequisite mentioned on CPAN.
What happens if you evaluate Email::Valid->address('redstone@nasa.gov') or some other string constant, email address that you know to be valid? I ask because perhaps you are having character encoding issues on your form submission. | [reply] |
|
perl 5.8.5.
'redstone@nasa.gov' evaluates correctly, as in Email::Valid->address('redstone@nasa.gov') returns the email address. An invalid email address returns nothing.
| [reply] |
Re: Validate newline delimited email list
by phaylon (Curate) on Jan 16, 2007 at 12:54 UTC
|
Just as a quick note, I would do:
my @addrs = split /\n/, $email_list;
my @addrs_invalid
= grep { not Email::Valid->address($_) }
@addrs;
print 'The following Email addresses are invalid:<ul>'
. (map { '<li>' . $_ . '</li>' } @addrs_invalid)
. '</ul>';
Untested and only proof of concept (no encoding and stuff), of course ;)
Ordinary morality is for ordinary people. -- Aleister Crowley
| [reply] [d/l] |