gpt4 book ai didi

c# - EmailAddress DataAnnotation 在 View 上标记为有效但 ModelState.IsValid = false?

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:01 24 4
gpt4 key购买 nike

我正在使用 MVC5 并尝试验证表单。

当我在字段中插入无效的电子邮件地址时,它会显示错误消息

enter image description here

如果我插入 a@a(这是一个有效的电子邮件地址),前端验证通过,但在后端,我的 ModelState 在 E-mail 字段上显示错误,告知该地址无效。

如果我插入 a@a.com,验证双方都会通过!

注意:大部分其他 SO 答案都与“电子邮件验证不适用于 MVC”相关,解决方案是使用我已经在使用的 EmailAddress 属性。

查看

@using (Ajax.BeginForm("EnviarMensagemContato", "home", new AjaxOptions { HttpMethod = "POST", OnBegin = "showLoading", OnComplete = "showJsonModalMessage" }, new { @id = "contact-form" }))
{
@Html.AntiForgeryToken()
<div class="col-md-6 lateral-division">
<div class="form-group">
@Html.TextBoxFor(m => m.ContatoViewModel.Nome, new { @class = "form-control required", placeholder = "nome" })
@Html.ValidationMessageFor(m => m.ContatoViewModel.Nome)
</div>
<div class="form-group">
@Html.TextBoxFor(m => m.ContatoViewModel.EmailAddress, new { @class = "form-control required", placeholder = "email" })
@Html.ValidationMessageFor(m => m.ContatoViewModel.EmailAddress)
</div>
<div class="form-group">
@Html.TextAreaFor(m => m.ContatoViewModel.Mensagem, 4, 4, new { @class = "form-control required", placeholder = "mensagem" })
@Html.ValidationMessageFor(m => m.ContatoViewModel.Mensagem)
</div>
</div>
<div class="btn-group pull-right btn-send-message">
<input type="submit" value="enviar" id="enviar-mensagem" class="btn btn-default" />
</div>
}

型号

public class ContatoViewModel
{
[Required(ErrorMessage="campo obrigatório"),
Display(Name = "nome")]
public String Nome { get; set; }

[Required(ErrorMessage = "campo obrigatório"),
Display(Name = "nome"), MaxLength(254,ErrorMessage="email inválido"),
EmailAddress(ErrorMessage="email inválido")]
public String EmailAddress { get; set; }

[Required(ErrorMessage = "campo obrigatório"),
Display(Name = "nome"), MaxLength(500, ErrorMessage = "maximo de 500 caracteres")]
public String Mensagem { get; set; }
}

Controller

public JsonResult EnviarMensagemContato(ModelContainer model)
{
try
{
if (ModelState.IsValid)
{
//zalgo
}
}
}

最佳答案

我假设您在 ASP.NET MVC 项目中使用默认的 jQuery Validation 库。如果是这种情况,问题在于用于验证电子邮件的正则表达式在 jQuery Validation 库和 EmailAddressAttribute 中是不同的,属性更多严格的。我的建议是重写 jQuery Validation 电子邮件方法,如下所示:

$.validator.methods.email = function (value, element) { 
return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/.test(value);
}

正则表达式直接取自EmailAddressAttribute源码here .如果您更愿意允许没有 TLD 的电子邮件(如 user@localhost),您可以按照讨论的方式编写自己的属性 in this answer并放入来自 jQuery Validation 库的正则表达式。这样,您将在客户端和服务器端获得相同的验证结果。

关于c# - EmailAddress DataAnnotation 在 View 上标记为有效但 ModelState.IsValid = false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com