为什么DataType.Password仅在服务器端验证?

 mobiledu2502931957 发布于 2022-12-29 14:04

我有模特儿

[Required]
[EmailAddress]
[Remote("EmailValidation", "Account", ErrorMessage = "{0} already has an account, please enter a different email address.")]
[Display(Name = "Email")]
public string Email { get; set; }

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

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

和注册表格:

@Html.AntiForgeryToken() @Html.ValidationSummary(true, String.Empty, new { @class = "text-danger text-center" }) @Html.HiddenFor(m => m.ForModal, new { @Value = true })
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Email", @readonly = "readonly" })
@Html.ValidationMessageFor(m => m.Email, null, new { @class = "text-danger" })
@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
@Html.ValidationMessageFor(m => m.Password, null, new { @class = "text-danger" })
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control", placeholder = "Confirm Password" })
@Html.ValidationMessageFor(m => m.ConfirmPassword, null, new { @class = "text-danger" })

当我输入短密码 - 验证火灾时,当我输入不同密码确认密码 - 验证时会触发,但是当我输入数字123456时,客户端没有任何反应,只是在服务器端,毕竟我得到错误.

http://i.imgur.com/jY8bymy.png(链接截图)

为什么我可以在客户端获得验证DataType.Password?


当我在互联网上搜索这样的问题时,我只是得到解决方案use Html.PasswordFor or Html.EditorFor instead of Html.TextBoxFor.

1 个回答
  • DataType属性不用于框外验证,主要用于渲染.例如,如果您用于EditorFor在内部呈现属性,它将使用该DataType属性来确定它应呈现的输入类型,即<input type="password" ... />.PasswordFor基本上做同样的事情,但不需要DataType属性.

    您的长度验证在客户端工作,因为它由StringLength属性处理DataType.如果你想强制执行特定的数据类型验证,我建议你看一下使用RegularExpressionAttribute,然后你会考虑实现自己的自定义验证器.

    2022-12-29 14:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有