2

I want to compare passwords and also a specified password length. There is some problem in the controller where I am updating it, it returns view only if the passwords are empty, then I applied condition to match them, but I want this to be done automatically like when I want to check the length I don't wnt to apply another condition in model. Please help me what is the problem with my approach?

I have tried this code

Controller:

    [AllowAnonymous]
    public ActionResult ResetPass()
    {
        return View();
    }

    [HttpPost]
    [AllowAnonymous]
    public ActionResult ResetPass(userPass model, string phoneNumber, String password, String repassword)
    {
        if (!String.IsNullOrEmpty(model.repassword) && !String.IsNullOrEmpty(model.password)&&model.password==model.repassword )
        {
            using (var db = new MongoContext())
            {
                db._database.GetCollection<userPass>("userPass");

                var filter = Builders<BsonDocument>.Filter.Eq("_phoneNumber", phoneNumber);

                var update = Builders<BsonDocument>.Update
                    .Set("password", password);

                db._database.GetCollection<BsonDocument>("farmers").UpdateOne(filter, update, null);

                return RedirectToAction("Login", new {Message = "password has been reset"});
            } 
        }
        return View(model);
    }

Model:

    public class userPass
    {
    [BsonElement("password")]
    [Required(ErrorMessage = "Password is required.")]
    [StringLength(8, ErrorMessage = "Password length must be 8.")]
    public string password { get; set; }

    [BsonElement("repassword")
    [Required(ErrorMessage = "Confirmation Password is required.")]
    [Compare("password", ErrorMessage = "Password and Confirmation Password must match.")]
    public string repassword { get; set; }
}

View:

              @using (Html.BeginForm("ResetPass", "Account", FormMethod.Post))
                {
                    <table>
                        <tr>
                            <td>Password</td>
                            <td>@Html.PasswordFor(m => m.password)</td>
                            <td>@Html.ValidationMessageFor(m => m.password, "", new { @class = "error" })</td>
                        </tr>
                        <tr>
                            <td>Confirm Password</td>
                            <td>@Html.PasswordFor(m => m.repassword)</td>
                            <td>@Html.ValidationMessageFor(m => m.repassword, "", new { @class = "error" })</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type="submit" class="btn btn-primary block m-b" value="Submit" /></td>
                            <td></td>
                        </tr>

                    </table>
                }

                @Scripts.Render("~/bundles/jquery")
                @Scripts.Render("~/bundles/jqueryval")
0

2 Answers 2

1
[HttpPost]
[AllowAnonymous]
public ActionResult ResetPass(userPass model)
{
    if(ModelState.IsValid) {

    }
    return View(model)
}

Just a little bit of modification.

For Minimum Length you can use range attribute -

[Range(8, 25, ErrorMessage = "Min Length should be 8")]

First paramter is minLength and second is maxLength

or

[MinLength(8, ErrorMessage = "Min Length should be 8")]
Sign up to request clarification or add additional context in comments.

5 Comments

Now it's working for comparing without writing condition in the controller and for required but not for the string length.
@artista_14 It's not Range, its StringLength, OP just has to define MinimumLength
@renu you're not providing MinimumLength
agree with @EdSF. you can use MinimumLength also
@artista_14 I can't upvote, this notification is displayed: Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.
1

Edit compare attribute.

 [Compare(CompareField = password, ErrorMessage = "Password and Confirmation Password must match.")]

here is detail example :

 public class userPass
 {
    [BsonElement("password")]
    [Required(ErrorMessage = "Password is required.")]
    [StringLength(8, ErrorMessage = "Password length must be 8.")]
    public string password { get; set; }

    [BsonElement("repassword")
    [Required(ErrorMessage = "Confirmation Password is required.")]
    [Compare(CompareField = password, ErrorMessage = "Password and Confirmation Password must match.")]
    public string repassword { get; set; }

}

Comments

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.