19

Is it possible to compare confirm password textbox's text with @Html.PasswordFor(model=>model.Password)?

@using (Html.BeginForm())
{
    <table>

        <tr>
            <td>@Html.LabelFor(model => model.Password)</td>
            <td>@Html.PasswordFor(model => model.Password)</td>
            <td>@Html.ValidationMessageFor(model => model.Password)</td>
        </tr>
        @*Here I want to take "Confirm Password" and want to compare it with "Password" in View(.cshtml only) as
          I have not taken ConfirmPassword in my model.*@
        <tr>
            <td>
                <input type="submit" value="Create" />
            </td>
        </tr>
    </table>              
}

Please suggest any way or solution,

How to compare password and confirm password without getting confirm password property in Model. Thanks....

7 Answers 7

28

Using Compare DataAnnotation it will be easy to compare password but if model genrate from database use NotMapped, NotMapped Properties In An Entity Framework Using A Code-First Strategy

[Required]
public string Password { get; set; }

[NotMapped] // Does not effect with your database
[Compare("Password")]
public string ConfirmPassword { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to compare confirm password textbox's text with @Html.PasswordFor(model=>model.Password)?
16

change your model to include confirm password variable

[Required]
public string Password { get; set; }
[Compare("Password")]
public string ConfirmPassword { get; set; }

2 Comments

Is it possible to compare confirm password textbox's text with @Html.PasswordFor(model=>model.Password)?
For me in better use nameof instead using a magic string antipattern. If you change the "Password" property you will have a runtime error...is not good. Try this: [Compare(nameof(Password), ErrorMessage = "Password doesn't match.")] public string ConfirmPassword { get; set; }
12

Just add [NotMapped] to above of your Confirm password Property in Data Model

[NotMapped]
[Required(ErrorMessage = "Confirm Password required")]
[CompareAttribute("NewPassword", ErrorMessage = "Password doesn't match.")]        
public string ConfirmPassowrd { get; set; }

By this way, it will not check ConfirmPassword property in your DB table

Comments

3

Just tried [Compare("field_to_compare")] and it also works in MVC 5.

Comments

1

Try to write javascript for that to compare password...

But DataAnnotation is Preferred

1 Comment

Although that would probably be enough to grandly limit the risks, client-side validation is not real validation...
1

It is possible to compare your "Password" text box value with a "Confirm Password" text box value both on client side and server side. The solutions given by others is for confirmation on server side. If you don't want to include "Confirm Password" in your model then you have to compare client side. This can be done through Javascript. Either you can manually write a code to compare or you can include the following script in your .cshtml file. (Assuming you are using Visual Studio to write your code).

<script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

Then you should create a field like below:

 <input  data-val="true" data-val-equalto="Password and Confirmation Password must match." data-val-equalto-other="*.Password" data-val-required="Required." id="ConfirmPassword" name ="ConfirmPassword"  type="password" />
<span class="field-validation-valid error" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>

This will compare your "Password" text box with "Confirm Password" text box and also show an error message if the values in both text box don't match, without you having to write any additional code.

Although, a good practice is to do both client side as well as server side validation.

Comments

0
 [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [NotMapped]
    [System.ComponentModel.DataAnnotations.Compare("Password",ErrorMessage ="Password and confirm password did not match.")]
    public string confirmpassword { get; set; }

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

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.