简体   繁体   中英

Should sfValidatorEmail not accept dots at the end of the local part of an email address?

There was a bug in my application, apparently dots at the end of the local part of an email address where not considered an error and the webservice I was using was telling me the email address was badly formatted.

sfValidatorEmail uses this regular expression:

const REGEX_EMAIL = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';

Dots at the end of the local part of an email address are apparently valid according to symfony 1.4

Is there any valid reason why sfValidatorEmail doesn't accept dots in the local part of an email address?

Does the problem come from the webservice I'm using?

UPDATE

Just read the RFC 3696 and here's what it says:

Without quotes, local-parts may consist of any combination of alphabetic characters, digits, or any of the special characters

  ! # $ % & ' * + - / = ? ^ _ ` . { | } ~ 

period (".") may also appear, but may not be used to start or end the
local part , nor may two or more consecutive periods appear.

Funny enough the original 1.0 releases of Symfony had a more extendable email validator. You may read the code at http://trac.symfony-project.org/browser/branches/1.0/lib/validator/sfEmailValidator.class.php?rev=6991

There were two options for the built in regex pattern, the one you mentioned above and then Cal Henderson's. I believe Cal's might fit your needs better and you may easily use it with Symfony's Regex Validator. I pasted the regex pattern and the explaining comments below.

/* Cal Henderson: http://iamcal.com/publish/articles/php/parsing_email/pdf/
 * The long regular expression below is made by the following code
 * fragment:
 *
 *   $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
 *   $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
 *   $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'
 *         . '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
 *   $quoted_pair = '\\x5c\\x00-\\x7f';
 *   $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
 *   $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
 *   $domain_ref = $atom;
 *   $sub_domain = "($domain_ref|$domain_literal)";
 *   $word = "($atom|$quoted_string)";
 *   $domain = "$sub_domain(\\x2e$sub_domain)*";
 *   $local_part = "$word(\\x2e$word)*";
 *   $addr_spec = "$local_part\\x40$domain";
 */

$re = '/^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-'
     .'\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-'
     .'\\x7f)*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-'
     .'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80'
     .'-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29'
     .'\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^'
     .'\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x5d)(\\x2e([^\\x00-'
     .'\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-'
     .'\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*'
     .'\\x5d))*$/'
;

I think it is also import to keep in mind that there are rare instances were a email address may not be RFC compliant, such as local intranet email addresses. Just a thought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM