Add a single validation message for multiple fields

advertisements

I have a password reset page that looks like this

I have a validation that verifies whether the new password has been used recently.

The requirement is that if the password has been used recently, then mark both new password and confirm password fields as erroneous and display validation message as follows.

How can I add a single validation message for the two fields?

I tried this

ModelState.AddModelError(newpasswordfieldname, "");
ModelState.AddModelError(confirmpasswordfieldname, AccountResources.FailMinPasswordHistory);

but that displayed an error "The value password is invalid." for the new password field.

Is there any way to achieve this, other than using TempData/ViewData, ie, using the native form validation SDK?


Did you try to use the AddModelError(String, String) overload?

ModelState.AddModelError(newpasswordfieldname, "This password was used too recently");
ModelState.AddModelError(confirmpasswordfieldname, "This password was used too recently");

Add the same custom message to both properties.