Search code examples
c#jqueryasp.net-core.net-6.0razor-pages

Why I get message "The password and confirmation password do not match"?


My model:

public class ResetPasswordViewModel
{
    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [RegularExpression(@"(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?\W)(?!.*\s).{8,24}$",
        ErrorMessage = "Password must be at least 8 and at max 24 characters long, contain at least one number and special character, have a mixture of uppercase and lowercase letters")]
    [DataType(DataType.Password)]
    public string NewPassword{ get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare(nameof(NewPassword), ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public int Code { get; set; }

    public bool Success { get; set; }
}

My partial pages:

<section class="page-content section-indent">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-8 col-md-10 col-md-offset-7 col-sm-12 col-sm-offset-6 col-xs-24 text-center login-logo">
                <img src="/images/logo/logo_blue.png" width="184" alt="Devart">
            </div>
        </div>
        <div class="row">
            <div class="col-lg-8 col-lg-offset-8 col-sm-12 col-sm-offset-6 col-xs-24 login-box">
                <div class="login-content">
                    <div class="login-title text-center">@Localizer["ResetPassword"]</div>
                    <form asp-action="ResetPassword" method="post" class="form" id="reset-password">
                        <div class="form-content">
                            @await Html.PartialAsync("_ResetPasswordPartial", Model)
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

@section scripts {
    @await Html.PartialAsync("_ValidationScriptsPartial")
}

_ResetPasswordPartial:

@model Models.ResetPasswordViewModel

@if (Model.Success)
{
    <div class="alert alert-info">
        <p>@Localizer["PasswordWasChanged"]</p>
    </div>
}
else
{
    <p class="login-info text-center">@Localizer["ResetPassword"]</p>

    <input asp-for="Code" type="hidden" />

    <div class="form-group">
        <label class="control-label">@Localizer["Email"]</label>
        <input asp-for="Email" class="form-control" maxlength="100" autofocus />
        <span asp-validation-for="Email" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label class="control-label">@Localizer["Password"]</label>
        <input asp-for="NewPassword" class="form-control" maxlength="100" />
        <span asp-validation-for="NewPassword" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label class="control-label">@Localizer["ConfirmPassword"]</label>
        <input asp-for="ConfirmPassword" class="form-control" maxlength="100" />
        <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
    </div>
    <div class="form-group text-center signin-button">
        <button type="submit" class="btn button--blue" value="login">@Localizer["Submit"]</button>
    </div>
}

My action:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<PartialViewResult> ResetPassword(ResetPasswordViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                await _accountInternalApiService.ChangeCustomerPasswordAsync(new ChangePasswordCustomerModel
                {
                    Email = model.Email,
                    Code = model.Code,
                    Password = model.NewPassword
                });
                model.Success = true;
            }
            catch (ApiException exception)
            {
                ModelState.AddModelError(nameof(model),
                    string.Format(_localizer["ErrorResetingPassword"], exception.Message));
            }
        }
        return PartialView("_ResetPasswordPartial", model);
    }

I use .Net 6 in project. Steps:

  1. Enter email, password, confirmation password old.
  2. I get error: I use old password and need use other. (error in catch action)
  3. Enter other password. Then I get message "The password and confirmation password do not match". But passwords equal. If I enter to confirmation password prev password I can send data but new password was enter other password

How can I fix it? Any ideas

I use jQuery validation. And it save old password in rules. How change new password in rule?


Solution

  • Problem was in js. Validation doesn`t reset.

    $('#reset-password').removeData('validator');
    $form.validate();