Is that a real email address? From a real domain?

Joomla > RSForms

In RSForms the out of the box validation for email fields checks that the value being submitted is just a correctly formatted email address. However, ID-10-T errors can sometimes mean that though the address is valid with the default check, it's not actually a valid address due to:

  • dropping a dot in the email suffix is valid, but the domain may not be. eg @kpsystems.comau vs @kpsystems.com.au
  • prefix being the wrong length or containing invalid characters
  • suffix being the wrong length or containing invalid characters

So to fix it, what's needed is a better validation routine for email.

And a bit of googling revealed an article to do just that at Linux Journal

I've made only a few little tweaks (one line actually - added ",$extra=null" to the function definition) to make it compatible with the rest of the RSForm validations, and it works well. The only thing it doesn't do is check the actual mail server to validate the email address exists, which may be possible with a bit more research.

To implement the function, add the following code to your /components/com_rsform/helpers/validation.php file and you'll then be able to validate both the format of the email address, and check that the domain exists for the email... and tell the user if it doesn't.

/**
* Validate an email address.
* Provide email address (raw input)
* Returns true if the email address has the email 
* address format and the domain exists.
* Taken from http://www.linuxjournal.com/article/9585?page=0,3
* RSForms version modifications Patrick Jackson KPS http://www.kpsystems.com.au
*/
function validEmail($email,$extra=null)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}
 

Add comment


Security code
Refresh