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.

1

1 Answer 1

1
// Disable keyup validation on key up
var validator = $("#yourFormId").data("validator");
if (validator)
    validator.settings.onkeyup = false;
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Not sure how to clear specific field's errors, but you should work on it in the focus (or click) event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.