asp.net-mvc – 如何在RegularExpression中忽略大小写?
发布时间:2020-12-30 23:54:35 所属栏目: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-classic – 如何使用AES在VBScript中进行加密?
- asp.net – 如何从Javascript调用控制器方法
- Jquery+ajax请求data显示在GridView上(asp.net)
- 什么是链接服务器的ASP.NET连接字符串格式?
- ASP.NET MembershipProvider加密/解密
- ASP.NET标签控件 – 不编码HTML
- 为什么asp.net会员资格有用户表和会员表?
- asp.net – IControllerFactory’MyWebSite.WebUI.Infrastr
- ASP.Net:为什么我的按钮的点击/命令事件没有在转发器中绑定
- .net – asp:GridView文本框始终返回空值
推荐文章
站长推荐
- 什么是ASP.NET WebForms相当于ASP.NET MVC的View
- asp.net-mvc – URL中的ASP.NET MVC冒号
- asp.net – 为什么HttpContext.Current.User.Ide
- jQuery validate 根据 asp.net MVC的验证提取简单
- asp.net-mvc – Asp.net 5(vnext)是否可以使用?
- asp.net-mvc – ViewModel中的最佳实践
- 如何为Asp.Net中的所有子文件夹注册HttpHandler?
- ASP.Net Forms身份验证在10分钟后注销用户
- asp.net-mvc – 如何使用ASP.NET MVC ApiControl
- asp.net-mvc – Gzip压缩无法运行ASP.net MVC5
热点阅读
