jQuery ASP.NET MVC 客户端电子邮件验证不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7242063/
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
ASP.NET MVC client-side e-mail validation doesn't work
提问by Chuck Norris
In my registration form I want to put some client-side validations with MVC regular expressions, but it doesn't work... Here is part of model's code
在我的注册表单中,我想使用 MVC 正则表达式进行一些客户端验证,但它不起作用......这是模型代码的一部分
[Required]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address*")]
public string Email { get; set; }
But the message don't want to appear anyway... What is wrong here?
但是该消息无论如何都不想出现......这里有什么问题?
EDIT
编辑
In my View I have this
在我看来,我有这个
<div>
<div class="editor-label">
@Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</div>
</div>
And I've also include jQuery libraries in my layout
我还在我的布局中包含了 jQuery 库
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
@RenderSection("Head")
</head>
回答by Chuck Norris
I've finally figured it out... Replacing Required attribute after RegularExpression attribute solved the problem)))
我终于想通了......在RegularExpression属性解决问题后替换Required属性)))
Instead this
而是这个
[Required]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address*")]
public string Email { get; set; }
now I have
我现在有
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address*")]
public string Email { get; set; }
And it now works perfectly!
现在它可以完美运行!
回答by Russ Cam
You also need
你还需要
1.@Html.ValidationMessageFor(m => m.Email)
in the view where you want the message to appear
1.@Html.ValidationMessageFor(m => m.Email)
在您希望消息出现的视图中
2.Include the JavaScript files required for client side validation. If you are using unobtrusive validation then this will be
2.包含客户端验证所需的 JavaScript 文件。如果您使用的是不显眼的验证,那么这将是
jQuery,
jQuery validate,
jQuery validate unobtrusive
in that order.
以该顺序。