简体   繁体   English

正常表达日期不起作用; MVC3 DataAnnotations

[英]Regular expression for date not working; MVC3 DataAnnotations

Im using DataAnnotations in an interface of a linq sql class. 我在linq sql类的接口中使用DataAnnotations。 Thats all fine. 多数民众赞成。

Im having problems with the date time fields 我有日期时间字段的问题

My code is as follows: 我的代码如下:

 [DataType(DataType.Date)]
    [RegularExpression(@"^([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$", ErrorMessage = "regexFail")]
    DateTime? DateofBirth { get; set; }

Now the datatype expression works fine, it brings in a date rather than a date time. 现在数据类型表达式工作正常,它带来日期而不是日期时间。 The problem lies in validation of the fields. 问题在于对字段的验证。 My regex doesn't match dates, even though i put it into an engine and it does. 我的正则表达式与日期不匹配,即使我将它放入引擎中也是如此。 For instance i put "10/10/2010" in the field and i get the error "regexFail". 例如,我把“10/10/2010”放在字段中,我得到错误“regexFail”。

I'm fairly sure my expression is good, so I'm not sure whats wrong. 我很确定我的表情很好,所以我不确定这是错的。

Thanks in advance. 提前致谢。

I think what is happening is that the DateTime value is being converted to a string, then matched against the pattern. 我认为发生的事情是将DateTime值转换为字符串,然后与模式匹配。 If that's the case, and ToString is being used, then a default time of 12:00:00 AM would be included in the string being matched. 如果是这种情况,并且正在使用ToString ,则默认时间12:00:00 AM将包含在匹配的字符串中。

I tried the following code and IsValid returned true : 我尝试了以下代码并且IsValid返回true

string pattern = @"^([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d\s12:00:00\sAM$";

RegularExpressionAttribute attribute = new RegularExpressionAttribute(pattern) { ErrorMessage = "regexFail" };
DateTime dt = new DateTime(2010, 10, 10);

bool isValid = attribute.IsValid(dt);

Decorating a DateTime field with the [RegularExpression] attribute doesn't make any sense. 使用[RegularExpression]属性装饰DateTime字段没有任何意义。 This attribute is used with string types. 此属性与字符串类型一起使用。 When you have a DateTime property the default model binder will use the current culture setting to parse the request value into a DateTime assuming it was a POST request and it will use the yyyy-MM-dd format if it was a GET request. 当您具有DateTime属性时,默认模型绑定器将使用当前区域性设置将请求值解析为DateTime,假设它是POST请求,并且如果它是GET请求,它将使用yyyy-MM-dd格式。 So as you can see it's the default model binder responsible for converting the user HTTP request into an instance of the DateTime field and the RegularExpression validator doesn't come into play. 因此,您可以看到它是默认的模型绑定器,负责将用户HTTP请求转换为DateTime字段的实例,并且RegularExpression验证器不起作用。

So if you want to restrict the dates that a user can enter to some custom format you could use a custom model binder for dates. 因此,如果您想限制用户可以输入某些自定义格式的日期,您可以使用自定义模型绑定器来表示日期。

Same stands true for other value types such as integers and doubles. 对于其他值类型(例如整数和双精度)也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 电子邮件的MVC3正则表达式 - MVC3 regular expression for email MVC3非侵入式验证不适用于自定义DataAnnotations属性 - MVC3 Unobtrusive Validation not working for custom DataAnnotations attribute 使用ComponentModel.DataAnnotations进行MVC3验证的英国日期格式(也使用jquery ui datepicker) - MVC3 Validation with ComponentModel.DataAnnotations for UK date format (also using jquery ui datepicker) DataAnnotations的FileExtensions属性在MVC中不起作用 - FileExtensions attribute of DataAnnotations not working in MVC 视图中的 MVC 4 正则表达式 Model - 日期 - MVC 4 Regular Expression in View Model - date ASP.NET MVC3 System.ComponentModel.DataAnnotations和Association - ASP.NET MVC3 System.ComponentModel.DataAnnotations and Association 构建MVC3 EditorFor模板时,是否可以访问DataAnnotations? - Is there a way to access DataAnnotations when building MVC3 EditorFor templates? 使用不显眼的 javascript / MVC3 和 DataAnnotations 验证电子邮件地址 - Validating an e-mail address with unobtrusive javascript / MVC3 and DataAnnotations DataAnnotations DisplayAttributes Order属性在MVC中不起作用 - DataAnnotations DisplayAttributes Order property is not working in MVC DataAnnotations中用于电子邮件验证的C#Regular Expression - 双反斜杠 - C# Regular Expression for email validation in DataAnnotations - Double backslash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM