Manual of PHP

 
 
 

Web map

 
rim   rim
arrowReady frontarrow of Codes PHParrow to Validate email in PHP

 

To validate email in PHP To print E-mail
To verify the validity of an e-mail address, that is to say, to validate it well writing of an email.

We are going to see a very useful function in PHP that serves to verify the validity of a mail. In fact he verifies if an e-mail address is well written syntactically, leaving aside the cross-checks if this mail exists or not really, that cannot be done so easily.

We are going to write a function that is called comprobar_email and receives the chain of text with the email that we want to validate. If saying email is correct from the syntactic point of view, that is to say, if it has a user's name, one 25 pound and a completion with the name of a domain or subdomain, etc, will return a 1, that is to say, really. In case the email is not written correctly, the function would return 0, which is equivalent to false.

The function if it gives done initially that the email is erroneous and realizes a series of cross-checks that, if they all answer correctly, give for conclusion that the email yes was well written. If some of these cross-checks was not correct, it would not go over at the end of the cross-checks and the result would stay as it has been supposed in the beginning, that is to say, how incorrectly.

code of the function

function comprobar_email ($email) {
   $mail_correcto = 0;
   //I verify a few first things
   if ((strlen ($email)> = 6) && (substr_count ($email, "@") == 1) && (substr ($email, 0,1)! = "@") && (substr ($email, strlen ($email)-1,1)! = "@")) {
      if ((! strstr ($email, "'")) && (! strstr ($email, "\" ")) && (! strstr ($email," \\")) && (! strstr ($email," \$ ")) && (! strstr ($email, ""))) {
         //I look if it has character.
         if (substr_count ($email, ".")> = 1) {
            //I obtain the completion of the domain
            $term_dom = substr (strrchr ($email, '.'), 1);
            //I verify that the completion of the domain is correct

            if (strlen ($term_dom)> 1 && strlen ($term_dom) <5 && (! strstr ($term_dom, "@"))) {
               //I verify that that thing about before the domain is correct

               $antes_dom = substr ($email, 0, strlen ($email) - strlen ($term_dom) - 1);
               $caracter_ult = substr ($antes_dom, strlen ($antes_dom)-1,1);
               if ($caracter_ult! = "@" && $caracter_ult! = ".") {
                  $mail_correcto = 1;
               }
            }
         }
      }
   }
   if ($mail_correcto)
      return 1;
   else
      return 0;
}


The cross-checks

In the first if I verify that the email has at least 6 characters (the minimum), that it has one 25 pound and the only one and that it is not placed not at first nor in the end.

In the second if he verifies that it does not have any not authorized characters. And the remaining ones do cross-checks of the different parts of the mail address, namely: That there is a point in some side and that the completion of the domain is correct and that the beginning of the direction also is correct.

Finally, he returns himself the local variable used for keeping the validity or incorrectness of the mail.

Miguel Angel Alvarez
http://www.desarrolloweb.com/articulos/990.php?manual=6

 

 
Front
Chapters of the Manual of PHP
Introduction to PHP
Variables
My First Script PHP
Operators in PHP
Structures of Control
Functions in PHP
Code inclusion
Counterfoils (array)
Chains of Characters
Classes
Dates
Entry and Exit
Operations with Files
The language SQL and PHP
Connection with MySQL
Meetings
Forms with PHP
To practise in line
List of practices in line
FAQ
Frequent questions
Codes PHP
List of Codes PHP
Forums
Forums PHP
Other Manuals
Manuals of other languages
 
   
 
 
MySpaceScripts.info - Sitemap - Contact us - Sitemap - Contact us - Sitemap - Contact us

Moto brands , Cool QSL Print Service , panasonic , Isla Mujeres properties , packaging , Samsung s800 jet , Hotels Booking