Check the email address format code

Source: Internet
Author: User
Tags ereg explode to domain
This is a code that fully complies with RFC2822 and RFC2821. Check only the single email address.
Function check_email_address ($ email ){
// First, we check that there's one @ symbol, and that the lengths are right
If (! Ereg ("[^ @] {1,255} @ [^ @] {}", $ email )){
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
Return false;
}
// Split it into sections to make life easier
$ Email_array = explode ("@", $ email );
$ Local_array = explode (".", $ email_array [0]);
For ($ I = 0; $ I <sizeof ($ local_array); $ I ++ ){
If (! Ereg ("^ ([A-Za-z0-9! # $ % & '* +/=? ^ _ '{| }~ -] [A-Za-z0-9! # $ % & '* +/=? ^ _ '{| }~. -] {0, 63}) | ("[^ (|")] {0, 62} ") $", $ local_array [$ I]) {
Return false;
}
}
If (! Ereg ("^ [? [0-9.] +]? $ ", $ Email_array [1]) {// Check if domain is IP. If not, it shoshould be valid domain name
$ Domain_array = explode (".", $ email_array [1]);
If (sizeof ($ domain_array) <2 ){
Return false; // Not enough parts to domain
}
For ($ I = 0; $ I <sizeof ($ domain_array); $ I ++ ){
If (! Ereg ("^ ([A-Za-z0-9] [A-Za-z0-9-] {} [A-Za-z0-9]) | ([A-Za-z0-9] +) $", $ domain_array [$ I]) {
Return false;
}
}
}
Return true;
}
RFC2822: defines the email format
Http://www.faqs.org/rfcs/rfc2822.html
RFC2821: defines the maximum length of the username and server name before and after @ In the email, section 4.5.3.1
Http://www.faqs.org/rfcs/rfc2821.html
In addition, RFC2234 defines ABNF and some commonly used terminal:
Http://www.faqs.org/rfcs/rfc2234.html
Most email address check codes on the Internet are not written in accordance with these RFC.
You can also add code to check whether the email server exists. This is available on the Internet.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.