12

I'm using the EmailAddressAttribute for use on my model.

The problem is when I use a (perfectly valid) e-mail address of

óscar@yahoo.com

it says it is invalid.

Model:

public class ForgotPasswordViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    public CmsContentPagesModel PageCmsContent { get; set; }

    public CmsContentPagesModel PageCmsContentInfoIcon { get; set; }
    public CmsContentPagesModel PageCmsContentRightPanel { get; set; }
}

Is this an issue with the attribute, or do I somehow have to specify that French e-mails are okay?

Input box as rendered:

<div class="col-md-5">
     <input class="form-control" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" type="text" value="" />
     <span class="field-validation-valid text-danger" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>

I've also extracted the regex from the client-side validation, the following line returns false

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( 'óscar@yahoo.com' );

which apparently complies with this standard even though the demo for this exact code also fails.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • 1
    The `EmailAddressAttribute` uses the following Regex to validate the value: [source](https://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/EmailAddressAttribute.cs,54). The email address you provide matches the regex, so it should be valid. Can you post the c# model? By the way, I recently wrote a blog post about [validating email address .NET](https://www.softfluent.com/blog/dev/2017/02/06/Advanced-email-address-validation-in-NET) – meziantou Feb 10 '17 at 17:00
  • @meziantou - That's good news, I've added the model. – m.edmondson Feb 10 '17 at 17:01
  • check this thread: http://stackoverflow.com/questions/12180753/regularexpressionvalidator-control-not-allowing-emails-with-french-characters-f – Md. Tazbir Ur Rahman Bhuiyan Feb 13 '17 at 11:00
  • The validation fails at the client-side using unobtrusive validation scripts? Any customization done in validation rules in the script for email or any other field which can cause conflict of rules? How you submit the form to the server, using AJAX or direct submit? – Chetan Feb 13 '17 at 11:10
  • Maybe you are hitting character codifications issues in the email text like treating UTF-8 as ISO-8859-1 or vice versa. Are you sure you are not trying to validate something like "óscar" or "xF3scar"...? – jlvaquero Feb 13 '17 at 11:17
  • How does email input looks like? – Leonid Vasilev Feb 13 '17 at 11:20
  • @ChetanRanpariya - Yes, it fails client side and the `jquery.validate.unobtrusive.js` library is loaded. I've added the input box HTML to the question – m.edmondson Feb 13 '17 at 11:37
  • I'm not sure if `ó` char can even be in an email address. Since it is non-ascii and everything. – AgentFire Feb 13 '17 at 11:43
  • @AgentFire it is valid.. ASCII is old.. Unicode is new.. Email/URL can now contain multi language characters – Abdul Hameed Feb 13 '17 at 11:48
  • 1
    But the client-side expression uses the pure ASCII range `a-zA-Z` which would appear to be a bug. – H H Feb 13 '17 at 12:06
  • Here is the commit in jquery validation library where they changed email regex to one from the spec: https://github.com/jquery-validation/jquery-validation/commit/dd162ae360639f73edd2dcf7a256710b2f5a4e64. Previous regex they used was actually working fine for your case so you might use it. You can also see that "If you don't like the implementation, report an issue against the HTML5 spec.". This regex is from spec itself, but I'm not sure it is correct because spec says that "A valid e-mail address is a string ... the character set for which is Unicode". Anyway you have to use custom regex. – Evk Feb 13 '17 at 12:33
  • @Evk - That's right! [I've raised an issue on the project](https://github.com/jquery-validation/jquery-validation/issues/1949). Though I may as well use the previous regex until resolved. – m.edmondson Feb 13 '17 at 12:34
  • Well they most likely will ask you to raise issue for spec itself, because regex is from spec even though it is conflicting with the text. Funny that in that same commit they changed unit tests with unicode emails to treat them as invalid :) – Evk Feb 13 '17 at 12:39
  • 1
    @m.edmondson Unicode mentioned in specification is irrelevant. Check this [specification issue](https://github.com/whatwg/html/issues/2357). – Leonid Vasilev Feb 13 '17 at 16:08

5 Answers5

5

You need to set custom regular expression for email validation in jQuery Validation Plugin:

$.validator.methods.email = function( value, element ) {
  return this.optional( element ) || /INSERT_YOUR_REGEX_HERE/.test( value );
}

jQuery Validation Plugin uses RFC 5322 Internet Message Protocol specification definition of a valid email. This definition disallows use of a non latin letters in local-part. Relevant excerpt from project's README.md:

IMPORTANT NOTE ABOUT EMAIL VALIDATION. As of version 1.12.0 this plugin is using the same regular expression that the HTML5 specification suggests for browsers to use. We will follow their lead and use the same check. If you think the specification is wrong, please report the issue to them. If you have different requirements, consider using a custom method. In case you need to adjust the built-in validation regular expression patterns, please follow the documentation.

Email address internationalization is defined by another specification - RFC 6530 Overview and Framework for Internationalized Email. It uses term SMTPUTF8. Check Email address page on Wikipedia for more information and support details.

Community
  • 1
  • 1
Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50
  • I went with this and used the regex from the used on the server by the `EmailAddressAttribute` so I can be sure client + server will agree. In addition I started a bug [over on the project](https://github.com/jquery-validation/jquery-validation/issues/1949) so at least those involved are aware. The elegance to this answer, meaning I can just include the code in a new .js file alongside the original is why I've awarded this bounty. Many thanks @Leonid Vesilyev, keep it up. – m.edmondson Feb 16 '17 at 09:51
2

As you can see in the regex:

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( 'óscar@yahoo.com' );

the ó character is not in the range

you can use the following regex:

/^[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-](?:[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-]{0,61}[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-])?(?:\.[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-](?:[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-]{0,61}[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-])?)*$/

And you can also creat a custom Attribute as FrenchEmailAddress in order to validate the property by using the regex I posted in the custom attribute

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FrenchEmailAddressAttribute : ValidationAttribute
{
    public string Pattern { get; set; }
    public RegexOptions Options { get; set; }

    public FrenchEmailAddressAttribute(string pattern, RegexOptions options = RegexOptions.None)
    {
        Pattern = pattern;
        Options = options;
    }

    public override bool IsValid(object value)
    {
        return IsValid(value as string);
    }

    public bool IsValid(string value)
    {
        return string.IsNullOrEmpty(value) ? true : new Regex(Pattern, Options).IsMatch(value);
    }
}
gwt
  • 2,331
  • 4
  • 37
  • 59
1

Context of the problem is very important to identifying the correct answer. In most scenarios, it is better to stick with the simple solution:

^\S+@\S+\.\S+$

This regex will run the risk of matching some invalid email addresses, but will enforce the general structure. Ideally you would be able to an email confirmation box or a confirmation email perform a second round of validation, but even without the additional validations, this should still catch the majority of unintentionally invalid entries.

user942620
  • 568
  • 6
  • 11
-1

System.ComponentModel.DataAnnotations.EmailAddressAttribute class using this regex.

const string pattern = @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$";

and validating your email address like said @ThomasRainero

levent
  • 3,464
  • 1
  • 12
  • 22
-4

I don't know how you validate the email address, I tried to validate your email like a string and my variable isValid has been validated with true.

EmailAddressAttribute testEmailAddress = new EmailAddressAttribute();
bool isValid = testEmailAddress.IsValid("óscar@yahoo.com");

Hope these is useful.

Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
  • @MarcelN.: It is a valid non-repro statement from before the question was extended. – H H Feb 13 '17 at 12:13