Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:58:36+00:00 2026-06-01T01:58:36+00:00

I’m working on a password reset function and trying to check to make sure

  • 0

I’m working on a password reset function and trying to check to make sure the new password is at least 7 characters. It will run and pass the new password to the controller and set it as the password for the user but it just uses whatever was entered instead of checking to make sure it meets the password requirements. Thanks for any suggestions 🙂

Here’s the model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace [CompanyName].Models
{
    public class ResetPasswordModel
    {
        [Required]
        [ValidatePasswordLength(7, ErrorMessage = "New passwords must be a minimum of 7 characters, please try a different password.")]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

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

And here’s the page to reset the password:

@model [CompanyName].Models.ResetPasswordModel
@{
    ViewBag.Title = "ResetPassword";
}

@if (Model == null)
{
    <p>
        We could not find your user account in the database.
    </p>
}
else
{

    <script type="text/javascript" src="../../Scripts/jquery.infieldlabel.min.js" ></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("label").inFieldLabels();
        });
    </script>
    <h2>
        Reset Password</h2>
   <p>
   Please enter your new password below.
   </p>
     <p>
        Note: New passwords are required to be a minimum of 7 characters in length.
    </p>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    using (Html.BeginForm())
    {
    <div style="position: relative;">
       <fieldset>
            <legend>Reset Password</legend>
        <label for="NewPassword" style="position:absolute; top: 24px; left: 16px;">New Password</label>



            <div class="editor-field">
                @Html.PasswordFor(m => m.NewPassword)
                @Html.ValidationMessageFor(m => m.NewPassword)
            </div>
            <br />
        <label for="ConfirmPassword" style="position:absolute; top: 64px; left: 16px;">Confirm New Password</label>     


            <div class="editor-field">
                @Html.PasswordFor(m => m.ConfirmPassword)
                @Html.ValidationMessageFor(m => m.ConfirmPassword)
            </div>
            <p>
                <input type="submit" value="reset Password" />
            </p>

        </fieldset>

    </div>
    }
}

Updated model code:

[Required]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        [StringLength(50, MinimumLength = 7, ErrorMessage="New passwords must be a minimum of 7 characters, please try a different password.")]
        public string NewPassword { get; set; }

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

Controller code:

public ActionResult ResetPassword(Guid secureID)
        {
            int id = secureID.FromSecureID();
            var model = new ResetPasswordModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model)
        {

            if (ModelState.IsValid)
            {
                int id = secureID.FromSecureID();
                var user = Database.Users.FirstOrDefault(u => u.ID == id);
                if (user == null)
                {
                    ModelState.AddModelError("ID", "Sorry! We could not find your user name in the database, please try again.");
                    return View(model);
                }
                //else (model.NewPassword == null) {
                //return View();
                //}
                user.PasswordHash = model.NewPassword.ToSha1Hash();
                Database.SubmitChanges();

            }
            return RedirectToAction("ChangePasswordSuccess");
        }

Updated controller code:

[HttpPost]
        public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model)
        {

            if(ModelState.IsValid)
               {  
                int id = secureID.FromSecureID();
                var user = Database.Users.FirstOrDefault(u => u.ID == id);
                if (user == null)
                {
                    ModelState.AddModelError("ID", "Sorry! We could not find your user name in the database, please try again.");
                    return View(model);
                }
                //else (model.NewPassword == null) {
                //return View();
                //}
                user.PasswordHash = model.NewPassword.ToSha1Hash();
                Database.SubmitChanges();
                return RedirectToAction("ChangePasswordSuccess");
            }

            return View(model); 
        }

Updated model code:

namespace [CompanyName].Models
{
    public class ResetPasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "New Password")]
        [StringLength(100, ErrorMessage = "The new must be at least 7 characters long.", MinimumLength = 7)]
        public string Password { set; get; }

        [Required]
        [DataType(DataType.Password)]
        [Compare("Password")]
        [Display(Name = "Confirm New Password")]
        public string ConfirmPassword { set; get; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:58:37+00:00Added an answer on June 1, 2026 at 1:58 am

    This Properties in your View model will take care of it.

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "New Password")]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 7)]
        public string NewPassword{ set; get; }
    
        [Required]
        [DataType(DataType.Password)]
        [Compare("Password")]
        [Display(Name = "Confirm New Password")]
        public string ConfirmPassword { set; get; }
    

    and in your controller action, you can check for the ModelState.IsValid property to see if the Validation failed or not

     [HttpPost]
     public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model)
     {
       if(ModelState.IsValid)
       {
          // Validation correct, Lets save the new password and redirect to another action
       }
       return View(model);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.