Skip to content

Luventas Web Design

A new blog for developers in PHP, Java and mobile Development

  • Home
  • 2011
  • Juli
  • 6
  • Email validation with PHP

Email validation with PHP

Posted on 6 Juli 20117 September 2011 By luventas Keine Kommentare zu Email validation with PHP
PHP

After I had posted you a validation script with JavaScript I will now give you the script for validating an email address with PHP. The JavaScript version was good for a validation directly while typing the address in the field. With this PHP script the address will be validated after sending the form to the server.
The script checks the address, if the length of the string is within the allowed borders, if it includes an @ sign, if the address part do not start or stops with a dot, if it do not have two consecutive dots, if there are no unallowed signs in the string and if the domain part is a valid domain.

Source code   
function validateEmailaddress($email) {
  $isValid = true;
  $indexOfAtSign = strrpos($email, "@");
  if (is_bool($indexOfAtSign) && !$indexOfAtSign) {
    //if no @ included, address is not valid
    $isValid = false;
  } else {
    // get domain part
    $domain = substr($email, $indexOfAtSign+1);
    // get specific address part
    $specific = substr($email, 0, $indexOfAtSign);
    // get length of the specific address part
    $specificLen = strlen($specific);
    // get length of domain part
    $domainLen = strlen($domain);
    if ($specificLen < 1 || $specificLen > 64) {
      // specific length part cannot be less then one sign
      // and not more than 64 signs
      $isValid = false;
    } else if ($domainLen < 1 || $domainLen > 255) {
      // domain part cannot be less then one sign
      // and not more than 255 signs
      $isValid = false;
    } else if ($specific[0] == '.' || $specific[$specificLen-1] == '.') {
      // specific address part cannot start or end with a dot
      $isValid = false;
    } else if (preg_match('/\\.\\./', $specific)) {
      // specific address part cannot have consecutive dots
      $isValid = false;
    } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
      // domain part has not valid signs included
      $isValid = false;
    } else if (preg_match('/\\.\\./', $domain)) {
      // domain part part cannot have consecutive dots
      $isValid = false;
    } else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
      str_replace("\\\\","",$specific))) {
      // character not valid in local part unless specific
      // address part is quoted
      if (!preg_match('/^"(\\\\"|[^"])+"$/',
        str_replace("\\\\","",$specific))) {
        $isValid = false;
      }
    }
    if ($isValid && !(checkdnsrr($domain,"MX") ||
      checkdnsrr($domain,"A"))) {
      // domain not found in DNS
      $isValid = false;
    }
  }
  return $isValid;
}

For validating the email address string you have to call this function with the address as parameter. It returns ‚true‘ if the address is a valid email address and ‚false‘ if not.
Source code   
if(validateEmailaddress('name@email.de')) {
  //do something
} else {
  // send user message, that address is not valid
}

 

Print Friendly, PDF & Email

Beitrags-Navigation

❮ Previous Post: Preserve your mail form for spam
Next Post: Send Email by Spring batch with variable attachments ❯

You may also like

PHP
Create thumbnail from image with PHP
25 Mai 2011
PHP
Create your website multilingual
29 Mai 2011
PHP
Create Random Password with PHP
23 Mai 2011
PHP
Create an Excel document with PHP
3 Juni 2011

Schreibe einen Kommentar Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Copyright © 2023 Luventas Web Design.

Theme: Oceanly News by ScriptsTown

Diese Website nutzt Cookies, um bestmögliche Funktionalität bieten zu können. OK, verstanden
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
immer aktiv
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SPEICHERN & AKZEPTIEREN