Gossamer Forum
Home : General : Perl Programming :

Two Emails Validation--Custom Errors--One Subroutine! How?

Quote Reply
Two Emails Validation--Custom Errors--One Subroutine! How?
Is there any way round to that i can write the following sub routine only once and check two different user input emails for validation with custom error messgas for each?

sub check_email {

$email = $in{'friends_email'} ;
if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/) {
return 0;
}
else {
return 1;
}
}

I am using the following code to call this sub routine:

if(!&check_email(email)){

Print "ERROR: Invalid Email Entered In \"Friend's Email\" Field";

}

Now i want to use the same sub routing to validate another user input email -- $in{'referrer_email'}. Is this possible in any manner? Or is there any other way round to doing it.

Presently I have written the same sub routine with another name and the first line of it changed to

$email = $in{'referrer_email'} ;


NOTE: I want to 'print' custom error message for each of them.
Quote Reply
Re: [Malik] Two Emails Validation--Custom Errors--One Subroutine! How? In reply to
An Answer to myself!

Dear Malik, U were a real idiot a few days back when u didnt know about the the default @_ parameter array. But now as u know the use of it (thanx a lot to Dan for his explanation) please change ur code to the following one.

sub check_email {

$email = $_[0];
if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/) {
return 0;
} else {
return 1;
}
}

Note the change of $in{'friends_email'} to $_[0]

Now u can pass the email address to the subroutine when u call it as follows

if(!&check_email($in{'friends_email'})){
$fremail = "\"Friend's E-Mail\"";
&Errormsg1;

} else {

if(!&check_email($in{'ref_email'})){
$refemail = "\"Your E-Mail\"";
&Errormsg1;

} else {
}

and here is ur custom error msg for both the emails...

sub Errormsg1 {

print "<font color='a00000' face=verdana size=2><b>ERROR: </font><font color='003366' face=verdana size=2>$fremail $refemail &nbsp;Field Invalid!</b><br><br>Please enter a valid e-mail address in <font color='a00000' face=verdana size=2><b>$fremail $refemail</b></font> Field.";

&footer;


exit;
}

Good luck for more perling.Wink

BTW thanx a lot to everybody whos been helpin me. It really helps!