-1

Right now I'm using

 [Display(Name = "Reason*")]
 [StringLength(int.MaxValue, MinimumLength = 100, ErrorMessage = "The reason must be at least 100 characters long")]
 public String reason { get; set; }

In my model I've set a minimum length validator of 100 characters for a textareafor in a form. However, the error message only prompts after I try to submit the form and it clears all the other filled out text fields in the form.

Is there a way to allow the error message to show on the view as the user is typing into the text field or another way to prevent this?

yfan183
  • 547
  • 2
  • 7
  • 20
  • You can use JavaScript to do on UI or you have to return your invalid model. Please show your controller code and we will help you with preventing to remove all data. – kat1330 Feb 22 '17 at 02:23
  • What do you mean _it clears all the other filled out text fields_? That is not the default behavior. Show the code in your POST method. –  Feb 22 '17 at 02:33
  • And have you added the relevant scripts for client side validation and generated the view correctly (if so, it works fine) –  Feb 22 '17 at 02:50

1 Answers1

1

What you want is mostly achieved through client-side validation. And most of the client side validations I have seen uses Javascript of some sort. So your keywords are "javascript, mvc5, validation".

If you are using a Javascript framework like jQuery, this answer might do the trick for you.

Note: I know this is not an answer-worthy answer, but my reputation does not allow me to comment.

Community
  • 1
  • 1
welrocken
  • 266
  • 1
  • 9