0

I need to validate the form only when I click the submit button and present the Error Messages based on the DataAnnotation's properties.

ViewModel:

 [Required(ErrorMessage = "Field Required")]
 [RegularExpression(@"^.{5,}$", ErrorMessage = "Message is to short")]
 public string Message { get; set; }

HTML:

 @Html.TextAreaFor(m => m.Message)
 @Html.ValidationMessageFor(m => m.Message)

What happens is that when I submit the form with an empty Message, I get the "Field Required" message, but when I start writing in the field, the error message change to "Message is to short" but I only want this when I press the submit button again.

Patrick
  • 2,995
  • 14
  • 64
  • 125
  • 1
    Is this what you are looking for? http://stackoverflow.com/questions/8022695/asp-net-mvc-3-jquery-validation-disable-unobtrusive-onkeyup – Diego Oct 15 '15 at 18:02

1 Answers1

1
// Disable keyup validation on key up
var validator = $("#yourFormId").data("validator");
if (validator)
    validator.settings.onkeyup = false;
Diego
  • 16,436
  • 26
  • 84
  • 136
  • Hi thanks! It works great! just one more question, do you think it's possible to clear the error message from the field just when, after showing the error on submit, I click in the field the first time? – Patrick Oct 15 '15 at 18:16
  • 1
    Not sure how to clear specific field's errors, but you should work on it in the `focus` (or `click`) event. – Diego Oct 15 '15 at 18:26