asp.net-mvc – 如何在RegularExpression中忽略大小写?
发布时间:2020-12-31 01:08:53 所属栏目:asp.Net 来源:互联网
导读:我有一个asp.net MVC应用程序.有一个称为File的实体,它有一个名为Name的属性. using System.ComponentModel.DataAnnotations;public class File { ... [RegularExpression(@([^.]+[.](jpg|jpeg|gif|png|wpf|doc|docx|xls|xlsx ...
我有一个asp.net MVC应用程序.有一个称为File的实体,它有一个名为Name的属性. using System.ComponentModel.DataAnnotations; public class File { ... [RegularExpression(@"([^.]+[.](jpg|jpeg|gif|png|wpf|doc|docx|xls|xlsx ...,ErrorMessage = "Invali File Name"] public string Name{ get; set; } ... } 有一个RegularExpressionValidator检查文件扩展名. 解决方法我可以想到的一种方法是编写自定义验证属性:public class IgnorecaseRegularExpressionAttribute : RegularExpressionAttribute,IClientValidatable { public IgnorecaseRegularExpressionAttribute(string pattern): base("(?i)" + pattern) { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context) { var rule = new ModelClientValidationRule { ValidationType = "icregex",ErrorMessage = ErrorMessage }; // Remove the (?i) that we added in the pattern as this // is not necessary for the client validation rule.ValidationParameters.Add("pattern",Pattern.Substring(4)); yield return rule; } } 然后用它装饰你的模型: [IgnorecaseRegularExpression(@"([^.]+[.](jpg|jpeg|gif|png|wpf|doc|docx|xls|xlsx",ErrorMessage = "Invalid File Name"] public string Name { get; set; } 最后在客户端上写一个适配器: <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> <script type="text/javascript"> jQuery.validator.unobtrusive.adapters.add('icregex',[ 'pattern' ],function (options) { options.rules['icregex'] = options.params; options.messages['icregex'] = options.message; }); jQuery.validator.addMethod('icregex',function (value,element,params) { var match; if (this.optional(element)) { return true; } match = new RegExp(params.pattern,'i').exec(value); return (match && (match.index === 0) && (match[0].length === value.length)); },''); </script> @using (Html.BeginForm()) { @Html.EditorFor(x => x.Name) @Html.ValidationMessageFor(x => x.Name) <input type="submit" value="OK" /> } 当然,您可以将客户端规则外部化为单独的JavaScript文件,以便您无需在任何地方重复. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET网址MAX_PATH限制
- asp.net-mvc-3 – 为MVC3应用程序配置Ninject的正确方法是什
- IIS和ASP.Net Web开发服务器之间的行为差异?
- asp.net-mvc – 使用ASP.Net MVC中的模型绑定器更新父/子记
- asp.net-mvc – 何时使用asp.net mvc的路由规则vs查询字符串
- asp.net – 在MVC 4.0中使用部分视图中的节
- asp.net-mvc – 如何组合两个dataTextFields的SelectList描
- .net – Viewstate隐藏字段如此之大,一切都会崩溃
- ASP.NET MVC学习教程之Razor语法
- asp.net文件上传解决方案(图片上传、单文件上传、多文件上
推荐文章
站长推荐
- 认证 – asp.net mvc 3:Page.User.IsInRole(“x
- 增加ASP.NET站点的executionTimeout和maxRequest
- ASP.NET MVC删除操作方法中的查询字符串
- asp.net – 如何在MVC 3中设置图表系列颜色?
- asp.net-mvc – ASP.NET MVC WebSite中的ERR_EMP
- ASP.NET Core 1.0 ConfigurationBuilder().AddJs
- asp-classic – 从Classic ASP执行存储过程
- asp.net-mvc – 文件上传MVC
- 在ASP.NET中下载文件的实现代码
- asp.net-mvc – 如何正确识别vs2008版本级别?
热点阅读