Reputation: 2781
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.
Upvotes: 0
Views: 2353
Reputation: 16714
// Disable keyup validation on key up
var validator = $("#yourFormId").data("validator");
if (validator)
validator.settings.onkeyup = false;
Upvotes: 1