I am using jquery datepicker on my view model
Here is my view:
@Html.TextBoxFor(o => o.JobStartDate, new { id = \"dt1\", @class = \"input-block-le
You can look at using the jquery globalize or add the following to your script (assuming of course that the server culture date format is 'dd/MM/yyy')
$.validator.addMethod('date', function (value, element) {
if (this.optional(element)) {
return true;
}
var valid = true;
try {
$.datepicker.parseDate('dd/mm/yy', value);
}
catch (err) {
valid = false;
}
return valid;
});
$('#dt1').datepicker({ dateFormat: 'dd/mm/yy' });
and please use @Html.ValidationMessageFor(m => m.JobStartDate)
You will need to create a custom validator. You need to modify your property to take a string value instead like this
public string JobStartDate {get; set; }
You will then need to create your custom validator like this
public class CheckDateAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
// Validate your Date here
}
}
After this you will just decorate your property as such -
[CheckDate]
public string JobStartDate {get; set;}
Here is a good tutorial on custom validators
Here MVC View Model:
public DateTime? date { get; set; }
This is for script :
$('.DatePickers').datetimepicker({
timepicker: false,
format: 'd-M-Y',
scrollInput: false,
closeOnDateSelect: true
});
keypress event for readonly date picker
$(".DatePickers").keypress(function (event) { event.preventDefault(); });