我在验证日期时间文本框时遇到问题,
日期时间是以下列方式显示的日期:
public class Article : TblAbstract<int> {
private DateTime displayDate;
[DisplayName("وقت الظهور")] // Display date in arabic
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd hh:mm:ss tt}")]
[DataType(DataType.DateTime)]
[Editable(true, AllowInitialValue = true)]
// the error message means: display date is not in a correct format and it must be 2016-09-30 06:26:35 am
[DateTimeFormatValidation(ErrorMessage = "وقت الظهور ليس بالشكل الصحيح، عليك إعتماد هذا الشكل 2016-09-30 06:26:35 am")]
public DateTime DisplayDate {
get { return displayDate; }
set { displayDate = value; }
}
}
public class AddArticleViewsModel : AdminGViewsModel {
private List<ArticleItem> articleItemList;
private Article newArticle;
public AddArticleViewsModel() : base(typeof(AddArticleViewsModel)) {
newArticle = new Article();
newArticle.DisplayDate = DateTime.Now;
articleItemList = new List<ArticleItem>();
}
public List<ArticleItem> ArticleItemList {
get { return articleItemList; }
set { articleItemList = value; }
}
public Article NewArticle {
get { return newArticle; }
set { newArticle = value; }
}
}
我在文本框中使用此jquery控件:
来自网站的jquery-ui-timepicker-addon-1.6.1.js
http://trentrichardson.com/examples/timepicker
使用的javascript:
<script>
$("#txtDisplayDate").datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm:ss tt'
});
</script>
文本框的html和razor:
<div class="lblMsg">
// arabic means: warning you to fix the following issues
@Html.ValidationSummary(false, "تنبيه، عليك إصلاح الأخطاء التالية:")
</div>
<p>
@Html.LabelFor(model => model.NewArticle.DisplayDate, new { id = "lblDisplayDate", name = "lblDisplayDate", Class = "lblAddArticle", For = "txtDisplayDate" })
@Html.TextBoxFor(model => model.NewArticle.DisplayDate, "{0:yyyy-MM-dd hh:mm:ss tt}", new { id = "txtDisplayDate", name = "txtDisplayDate", Class = "inputAddArticle" })
</p>
除了FIREFOX(Firefox 43.04 64位版)
之外,所有浏览器均可正常使用见这些图片:
设计页面的图片有问题: i.stack.imgur.com/EMLa4.png
图片显示firefox中发生的问题: i.stack.imgur.com/UGKRc.png
答案 0 :(得分:0)
问题的解决方案是使用: 剃刀:强>
@Html.TextBoxFor(model => model.NewArticle.DisplayDate, "{0:yyyy-MM-dd hh:mm:ss tt}", new { id = "txtDisplayDate", name = "txtDisplayDate", Class = "inputAddArticle", @Type="datetime", @Data_Val_Date = "وقت الظهور ليس بالنسق المطلوب، عليك إعتماد هذا الشكل 2016-01-30 11:30:20 am" })
<强> JavaScript的:强>
$(function () {
jQuery.validator.methods.date = function (value, element) {
if (value) {
try {
var dateTest = /^[1-2][0-9]{3}-[0-1][1-9]-[0-3][0-9] [0-1][1-9]:[0-5][0-9]:[0-5][0-9] [APap][mM]$/;
return this.optional(element) || dateTest.test(value);
} catch (ex) {
alert(ex);
return false;
}
}
return true;
};
}