Confirmation date in datepicker

I have two datepickers:

$(".From").datepicker({
dateFormat: 'dd/mm/yy',
numberOfMonths: 1,
autoclose: true,
onClose: function(selectedDate) {
    $(".To").datepicker("option", "minDate", selectedDate);
}});


$(".To").datepicker({
dateFormat: 'dd/mm/yy',
numberOfMonths: 1,
autoclose: true,
onClose: function(selectedDate) {
    $(".From").datepicker("option", "maxDate", selectedDate);
}});

View:

 @Html.TextBoxFor(model => model.From, new { @class = "form-control From" })
 @Html.TextBoxFor(model => model.To, new { @class = "form-control To" })

Model:

    [Display(Name = "From")]
    public string From { get; set; }

    [Display(Name = "To")]
    public string To { get; set}; 

When a user manually enters a date, he fails.

Is there a way to allow users to enter a date and check it?

option 1: Should I use regex?

+4
source share
1 answer

Here is a working example

Model

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode=true)]
[DisplayName(Name = "From")]
public DateTime? From { get; set; }  // note the data type

View

@Html.TextBoxFor(model => model.From, new { @class = "form-control From" })
@Html.ValidationMessageFor(model => model.From)

Remember to download validation scripts .

jquery.validate.js
jquery.validate.unobtrusive.js 

This will give you a validation error message when you try to enter a bad date.

+1
source

All Articles