Perl: e-mail address validation function

Get help with nopcart Perl, PHP or ASP checkout related issues.

Moderators: Koibito, Stefko, Randy

Perl: e-mail address validation function

Postby Koibito » Sat Mar 08, 2008 1:30 am

We had a customer complaining about not being able to submit an order, because the Perl checkout script decided that her e-mail address was invalid. Her e-mail address started with four digits (like in: 1234user@host.com), which is perfectly valid according to RFC 2822 (http://tools.ietf.org/html/rfc2822) and RFC 2821 (http://tools.ietf.org/html/rfc2821).

When I looked at the e-mail validation function of the standard Perl checkout script, I saw why this happened. I changed the regexp, so that it will validate e-mail addresses starting with digits:

Code: Select all
sub invalidE {
  my ($szEmail) = @_;
  my ($user, $host);

  $szEmail =~ tr/A-Z/a-z/;
  if ($szEmail =~ /\s/) { return 1; }
  ($user, $host) = split (/\@/, $szEmail);
  if ($host =~ /compuserve/i) { ; }
  else {
    if (! $user =~ /\D/) { return 1; }
    if (! $host =~ /\D/) { return 1; }
    # if (substr ($user,0,1) !~ /[a-z]/) { return 1; }
    if (substr ($user,0,1) !~ /[a-z0-9]/) { return 1; }
  }
  if ($szEmail =~ /\w+\@[\w|\.]/) { return 0; }
  else { return 1; }
}


If you wish, you could also use the following elegant function, which I found at the ActiveState site:

Code: Select all
sub ValidEmailAddr { #check if e-mail address format is valid
  my $mail = shift;                                                  #in form name@host
  return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ ); #characters allowed on name: 0-9a-Z-._ on host: 0-9a-Z-. on between: @
  return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);             #must start or end with alpha or num
  return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );                    #name must end with alpha or num
  return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );                    #host must start with alpha or num
  return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );           #pair .- or -. or -- or .. not allowed
  return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );    #pair ._ or -_ or _. or _- or __ not allowed
  return 0 if ( $mail !~ /\.([a-zA-Z]{2,3})$/ );                     #host must end with '.' plus 2 or 3 alpha for TopLevelDomain (MUST be modified in future!)
  return 1;
}
John
Koibito
Site Admin / Guru
 
Posts: 918
Joined: Sun May 28, 2006 1:59 am
Location: New Jersey, USA

Re: Perl: e-mail address validation function

Postby trader4300facebook » Wed Oct 03, 2012 2:10 am

What do you suggest using for the PHP users (non-perl) for this implementation?
trader4300facebook
WebMaster
 
Posts: 37
Joined: Sat Sep 29, 2012 5:08 pm

Re: Perl: e-mail address validation function

Postby Koibito » Wed Oct 03, 2012 2:48 am

For PHP:

Code: Select all
if (preg_match('|^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $b_email)) {
    // e-mail is valid
} else {
    doFormError("You submitted an invalid email address.  Please go <a href='Javascript:history.go(-1);'>back</a> and correct the error.");
    exit;
}


'ereg' and 'eregi' are deprecated functions.
John
Koibito
Site Admin / Guru
 
Posts: 918
Joined: Sun May 28, 2006 1:59 am
Location: New Jersey, USA

Re: Perl: e-mail address validation function

Postby trader4300facebook » Wed Oct 03, 2012 11:04 am

Great! That seems to work just fine as a replacement for EREG. Thank-you!!!
trader4300facebook
WebMaster
 
Posts: 37
Joined: Sat Sep 29, 2012 5:08 pm


Return to Help: Perl/PHP/ASP Checkout

Who is online

Users browsing this forum: No registered users and 0 guests