I didn't use jquery.ui's datepicker for one because I needed to select multiple dates, so I am using Keith Wood's datepicker in an EditorTemplate for my 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.Hidden("", dt, new { @class = "datefield", @type = "date", @id = id, style = "display:none;" })
<script type="text/javascript">
$(document).ready(function () {
$('#@id').datepick({
renderer: $.datepick.themeRollerRenderer,
multiSelect: 31,
onSelect: function (dates) {
var value = '';
for (var i = 0; i < dates.length; i++) {
value += (i == 0 ? '' : ',') + $.datepick.formatDate(dates[i]);
}
$('#@name').val(value)
var form = $(this).parents().find('form')[0];
// form.validate(); // this causes error (Object doesn't support property or method 'validate') whats up with that?
}
});
});
</script>
And here is the definition of the DateTime field in my Data Class in which my ViewModel inherits:
[Required]
[DataType(DataType.Date)]
public Nullable<DateTime> StartDate { get; set; }
And here is the client side validation snip-it which is returning an invalid status:
form.validate({
rules:{
StartDate: {
required: true
,dpDate: true
}
},
messages: {
StartDate: 'Please choose at least one date'
}
});
if (form.valid()) { // never reached }
So now I'm finalizing the last bit of validation for this inline datepicker. As you see I have a hidden field that is populated by the datepicker and it contains valid dates, but comma delimited and which does not translate to a datetime. It is valid when I choose just a single date. Is there a generic solution for validating the delimited data as a type?
I am basically looping a database call using the multiple dates but would still like to validate that the delimited data is of type DateTime.
Regards,
Brandon