asp.net-mvc 在 ASP.Net MVC 中比较密码和确认密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21746910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare password and confirm password in ASP.Net MVC
提问by Vishal Hirapara
Is it possible to compare confirm password textbox's text with
@Html.PasswordFor(model=>model.Password)?
是否可以将确认密码文本框的文本与
@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 passwordand confirm passwordwithout getting confirm password property in Model. Thanks....
如何compare password和confirm password不获取模型中的确认密码属性。谢谢....
回答by Sender
Using CompareDataAnnotationit 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
使用CompareDataAnnotation它会很容易比较密码,但如果模型从数据库使用中生成NotMapped,使用代码优先策略的实体框架中的 NotMapped 属性
[Required]
public string Password { get; set; }
[NotMapped] // Does not effect with your database
[Compare("Password")]
public string ConfirmPassword { get; set; }
回答by COLD TOLD
change your model to include confirm password variable
更改您的模型以包含确认密码变量
[Required]
public string Password { get; set; }
[Compare("Password")]
public string ConfirmPassword { get; set; }
回答by Steve
Just add [NotMapped]to above of your Confirm password Property in Data Model
只需添加[NotMapped]到数据模型中确认密码属性的上方
[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 ConfirmPasswordproperty in your DB table
通过这种方式,它不会检查ConfirmPassword您的数据库表中的属性
回答by Bogdan Mates
Just tried [Compare("field_to_compare")]and it also works in MVC 5.
刚刚尝试过[Compare("field_to_compare")],它也适用于 MVC 5。
回答by Nilesh Gajare
Try to write javascriptfor that to compare password...
尝试写下javascript比较密码...
But DataAnnotationis Preferred
但是DataAnnotation是首选
回答by Omkar
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).
可以将您的“密码”文本框值与客户端和服务器端的“确认密码”文本框值进行比较。其他人给出的解决方案是为了在服务器端确认。如果您不想在模型中包含“确认密码”,则必须比较客户端。这可以通过 Javascript 来完成。您可以手动编写代码进行比较,也可以在 .cshtml 文件中包含以下脚本。(假设您使用 Visual Studio 编写代码)。
<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.
尽管如此,一个好的做法是同时进行客户端和服务器端验证。

