使用自定义格式进行日期时间验证。与''纠缠。分隔符
在我的web应用程序(asp.new mvc)中,我尝试对日期字段进行验证。使用自定义格式进行日期时间验证。与''纠缠。分隔符
如果我用这个
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
或本
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
它的工作原理。但是,如果我尝试使用'。'分隔。分离器
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
我有个问题。 jQuery验证说,如果我尝试'01 .01.2001',我必须输入日期。
这里是视图
<div class="ElementDiv">
@Html.LabelFor(m => m.Filter.PeriodBeginFrom, new { @class = "Labels" })
@Html.EditorFor(m => m.Filter.PeriodBeginFrom, new { @class = "TextBoxes" })
@Html.ValidationMessageFor(m => m.Filter.PeriodBeginFrom, null, new { @class = "Errors" })
</div>
为什么?与所有其他分离器一起工作。 ''有什么问题'。' separatior?
更新1:看来验证无法正常工作。如果我在我的模型中具有此属性,则我们将DateTime对象传递给模型,它将显示在带有'。'的文本框中。就像'12。2012年12月',而不是像我在等待'2012年12月12日'。希望你能给我一些想法。
问题与格式不与ASP标记和字段,但与不显眼的验证jQuery库。我不得不为日期格式重载不显眼的JavaScript验证方法。更多细节在这里 MVC DateTime validation - UK Date format
您正在查看日期组件。但请记住,时间组件可以包含秒和小数秒,并包含一段时间。由于日期和时间经常合并,所以如果这段时间被视为特殊字符,我不会感到惊讶。
更新:
DisplayFormatAttribute.ApplyFormatInEditMode Property
DisplayFormatAttribute.DataFormatString Property
Custom Date and Time Format Strings
:我通过以下文档链去0
我期望看到句点字符在格式字符串中有特殊含义。相反,该文档没有具体列出周期字符。这意味着它应该属于“任何其他字符”的类别,因此,“字符被复制到结果字符串不变”。
但是,我不相信。小时秒会出现在23:59:59.999等时间分量中,我怀疑这个周期字符没有特殊处理。
请解释一下你的意思。我把日期放在没有时间的模型上,如果我们看调试器,时间总是是12:00。 – Vitalii 2013-04-23 20:53:37
@Vitalii:我不是说你在使用时间组件。相反,我的意思是,由于时间组件可能包含句点字符,因此编译器可能会不同地处理格式字符串中的句点字符。但目前MSDN文档尚不支持此功能,请参阅我的更新。 – criticalfix 2013-04-23 21:14:17
也许你可以用'\\'来避开这个点? – 2017-01-17 10:55:48