3

I have a simple Input Model that has an Email property on it. I'm using the EmailAddress DataAnnotation to validate the input data.

[Required]
[Display(Name = "UserReview_CustomerEmailTitle", ResourceType = typeof(Resources.User.Settings))]
[EmailAddress(ErrorMessageResourceType = typeof(Resources.User.Settings), ErrorMessageResourceName = "UserReview_InvalidEmailMessage")]
public string CustomerEmail { get; set; }

It works perfectly (ie. data is valid) if there are no spaces before/after the input data.

When I have a space at the end of the input text, it fails the validation.

Is there any tricks I can do to TRIM the input data coming in OR tell the EmailAddress validator to ignore prepended/postpended spaces?

EDIT:

I'm open to creating a custom attribute... I can't inherit from the EmailAddress attribute because it's sealed (sigh) .. so maybe a regex attribute?

Community
  • 1
  • 1
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

3 Answers3

3

You could handle in javascript when user tabs out of the email field to run trim on the value and replace it. EmailAddress annotation plays nice with unobtrusive validation on the client side (assuming MVC here).

If you want to avoid client side, you might look into a custom model binder where you could manipulate the data used to create the model-to-be-validated. You could check out some tutorials here.

Last but not least, maybe you could borrow the regex used within the Email validator and make it accept whitespace via a new RegularExpression validator and handle the TRIM in your code explicitly.

Community
  • 1
  • 1
felickz
  • 4,292
  • 3
  • 33
  • 37
  • Can't do client side manipulation unfortunately. Server side is safest. – Pure.Krome Apr 27 '16 at 04:58
  • The regex solution could be the easiest right now ... any link to that? – Pure.Krome Apr 27 '16 at 05:08
  • as in .. any link to the email _regex_ pattern... :) (i know about the attribute) – Pure.Krome Apr 27 '16 at 05:10
  • @Pure.Krome it is a nice one :) [github!](https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/Microsoft.Web.Mvc/EmailAddressAttribute.cs) ... no idea why but the [coreFX one](https://github.com/dotnet/corefx/blob/master/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs/#L19) is rather lame.. – felickz Apr 27 '16 at 05:16
2

If you are able to make changes to the model, you could add another property and validate it:

[Required]
public string CustomerEmail { get; set; }

[Required]
[Display(Name = "UserReview_CustomerEmailTitle", ResourceType = typeof(Resources.User.Settings))]
[EmailAddress(ErrorMessageResourceType = typeof(Resources.User.Settings), ErrorMessageResourceName = "UserReview_InvalidEmailMessage")]
public string CustomerEmailTrimmed => CustomerEmail?.Trim();

Had a similar problem on ASP.NET Core 2.2.

thomaswr
  • 524
  • 1
  • 9
  • 19
0

Try CustomerEmail.Trim(). It does exactly what you describe.

See https://msdn.microsoft.com/en-us/library/t97s7bs3(v=vs.110).aspx

Kelvin Lai
  • 2,209
  • 7
  • 24
  • 26