View validation of a model that ignores leading and trailing spaces

I have the following presentation model.

public UserViewModel { ... [Email(@"^ .+@ [^\.].*\.[az]{2,}$", false, ErrorMessage="...")] public string EmailAddress{ get; set; } ... } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Visitor(UserViewModel userViewModel) ... 

However, I found that sometimes the user copies and pastes the email into the view from another mailbox, text document, etc., and sometimes it captures the leading and / or ending space. Since users are not the most intelligent, and spaces are not displayed in the input control, then I must ignore them and continue.

So if my model state is invalid, I wonder how best to solve this -

  • Add spaces to my expression for verification.
  • In the action result method, check if you delete the email address and remove the error from the model state manually.
  • Do something else ...

I'm not 100% sure about 1, and 2 is honest too!

+6
source share
2 answers

I really would not add spaces to the expression to test.

Take a look at this answer: ASP.NET MVC: the best way to trim lines after data entry. Should I create a custom mediator?

+3
source

You can change the settings to have a trim code when setting the value in the property too.

+2
source

Source: https://habr.com/ru/post/901510/


All Articles