I did not use jquery.ui datepicker because I needed to select a whole week and several dates under different conditions, so I use Keith Wood's datepicker in the EditorTemplate field for Date fields:
@model Nullable<DateTime>
@{
string name = ViewData.TemplateInfo.HtmlFieldPrefix;
string id = name.Replace(".", "_");
string dt = Model.HasValue ? String.Format("{0:d}",(string)Model.Value.ToShortDateString()) : string.Empty;
}
@Html.TextBox("", dt, new { @class = "datefield", @type = "date", @id = id })
<script type="text/javascript">
$(document).ready(function () {
$('#@id').datepick({
renderer: $.datepick.themeRollerRenderer,
multiSelect: 999
});
});
</script>
The check works correctly when using a text field and has a datepicker popup; but I really want to use the built-in datepicker, but I can’t get confirmation to work in these conditions:
@model Nullable<DateTime>
@{
string name = ViewData.TemplateInfo.HtmlFieldPrefix;
string id = name.Replace(".", "_");
string dt = Model.HasValue ? String.Format("{0:d}",(string)Model.Value.ToShortDateString()) : string.Empty;
}
<div class="kwdp">
</div>
@Html.Hidden("", dt, new { @class = "datefield", @id = id })
<script type="text/javascript">
$(document).ready(function () {
$('.kwdp').datepick({
renderer: $.datepick.themeRollerRenderer,
multiSelect: 999
});
});
</script>
Here is an example of my client side custom validation:
form.validate({
rules:{
StartDate: {
required: true,
dpDate: true
}
},
messages: {
StartDate: 'Please enter a valid date (dd-mm-yyyy)'
}
});
And here is the definition of the DateTime field in my data class:
[Required]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public Nullable<DateTime> StartDate { get; set; }
, onSelect datepicker, (), , , , . html :
<input name="StartDate" class="datefield" id="StartDate" type="hidden" data-val-required="The StartDate field is required." data-val="true" data-val-date="The field StartDate must be a date." value=""/>
:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/datepick/jquery.datepick.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/datepick/jquery.datepick.ext.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/datepick/jquery.datepick.validation.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.datepicker.validation.min.js")" type="text/javascript"></script>
, , , . , ?