1

I have a password reset page that looks like this

enter image description here

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.

enter image description here

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?

5
  • If I understand your question correctly you just need to compare the new password and the recent one. how about comparevalidator field? im not sure in asp.netMVC Commented Nov 20, 2015 at 8:15
  • The validation workflow works fine, the only issue i face is displaying the validation result Commented Nov 20, 2015 at 8:16
  • I see, why not use jquery? I use it to manipulate css from code behind however im not yet familiar with MVC. Commented Nov 20, 2015 at 8:19
  • you want the same message on two fields? Commented Nov 20, 2015 at 8:27
  • I want no error message in first field, just the color. Commented Nov 20, 2015 at 8:30

2 Answers 2

1

Since I found no solution using the Native MVC validation SDK, I was forced to use a dirty workaround.

In my controller

ModelState.AddModelError(newpasswordfieldname, "DONOTSHOW");
ModelState.AddModelError(confirmpasswordfieldname, AccountResources.FailMinPasswordAge);

And in my view

@if(ViewData.ModelState["NewPassword"]!=null && 
    ViewData.ModelState["NewPassword"].Errors.First().ErrorMessage!="DONOTSHOW")
{
    @Html.ValidationMessageFor(model => model.NewPassword)
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

That will display the message twice. And FYI, I use the string overload.
Ohh, ok. In that case, probably simplest would be to add with jQuery the class 'has-error' or similar to the input field. You can either send over a bool variable to find out if you need to use it or make it depend on the error message of the confirm password prop.
Or if validation message if same as the other, set to display:none. or any of those tricks.
I thought of all those, but those don't look so neat :). If that's the only solution, I can only see this as a short fall of the MVC framework.

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.