Hell, I want to add manually date in Text box with date format dd/MM/yyyy.. I have declared model validation, but i have found that MVc will not take part for Date validation, need to do with jquery, I have done like that but. here my some cases not wokring for example if i am entering 10/12/20 then i need to show the message dateformat is wrong as i want 10/12/2013. I am using also Bootstrap date picker..but it is hard to select old date using boostratp datepicker.. so i need to add manually date with Date Format validation.
//Model
[Required(ErrorMessage = "Birth Date is required")]
//[Display(Name = "When where you born?")]
[Display(Name = "Birth Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? BirthDate { get; set; }
Below is Jquery
//Jquery
var dateFormat = "dd/mm/yy";
$("#BirthDate").datepicker({
dateFormat: dateFormat,
autoclose: true,
orientation: 'top left'
})
.on('changeDate', function (ev) {
var dob = new Date($("#BirthDate").val());
var today = new Date();
var Age = parseInt(today.getFullYear()) - parseInt(dob.getFullYear());
$("#EighteenAge").val('');
$("#EighteenAge").val(Age);
});
$(function ($)
{
$('#BirthDate').on("keypress", function (e)
{
ValidDate();
});
});
function ValidDate()
{
$.validator.addMethod('date',
function (value, element)
{
if (this.optional(element))
{
return true;
}
var ok = true;
try
{
$.datepicker.parseDate(dateFormat, value);
}
catch (err)
{
ok = false;
}
return ok;
});
}
please help some