6

I have the following view model.

public UserViewModel {
 ...
 [Email(@"^.+@[^\.].*\.[a-z]{2,}$", false, ErrorMessage="...")]
 public string EmailAddress{ get; set; }
 ...
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Visitor(UserViewModel userViewModel)
...

However I have found that sometimes a user copies and pastes an email into the view from another email, word document etc and sometimes it picks up a leading and/or trailing space. As users are not the most intelligent and spaces do not show in the input control then really I should ignore these and carry on.

So if my model state is invalid, I am wondering the best approach to resolve this-

  1. Add spaces to my validation expression.
  2. In the action result method check if trim the email and remove the error from the modelstate manually.
  3. Do something else...

I am not 100% sure about 1, nor 2 too be honest!

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • Possible duplicate of [Best way to trim strings after data entry. Should I create a custom model binder?](https://stackoverflow.com/questions/1718501/best-way-to-trim-strings-after-data-entry-should-i-create-a-custom-model-binder) – Liam May 18 '18 at 13:13
  • I really would not add spaces to your validation expression. Have a look at this answer: https://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custo – kay.herzam Nov 15 '11 at 13:14

1 Answers1

1

you can change the setters to have the trim code upon setting the value in the property too.

Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31