1

I am trying to validate datepicker as startDate should be always less than EndDate. If I change the date manually then StartDate works but EndDate does not work.

My JavaScript Code:

$(document).ready(function () {
       $("#StartDate").datepicker({
        numberOfMonths: 1,
        onSelect: function (selected) {
            $("#EndDate").datepicker("option", "minDate", selected)
        }
    });
      $("#EndDate").datepicker({
       numberOfMonths: 1,
        onSelect: function (selected) {
            $("#StartDate").datepicker("option", "maxDate", selected)
        }
    });

});

My .cshtml code

 <div class="col-sm-6">
    <div class="form-group">
        @Html.LabelFor(m => m.StartDate)
        @Html.TextBoxFor(m => m.StartDate, new { @class = "form-control", placeholder = "Enter start date" })
        @Html.ValidationMessageFor(m => m.StartDate, "", new { @class = "error" })
    </div>
</div>

<div class="col-sm-6">
    <div class="form-group">
        @Html.LabelFor(m => m.EndDate)
        @Html.TextBoxFor(m => m.EndDate, new { @class = "form-control", placeholder = "Enter finish date" })
        @Html.ValidationMessageFor(m => m.EndDate, "", new { @class = "error" })
    </div>
</div>

Anyone please help me. Thank you in advance!

8
  • I don't see any problem with your code. Can you add the entire view and your file reference?? Commented Jan 4, 2018 at 5:07
  • @raj below is running example Commented Jan 4, 2018 at 7:53
  • @JibinBalachandran,In normal circumstance, it is working perfectly fine. But the problem arises only when we change EndDate manually. First please change start date manually by writing any date like 01/16/2010 before current date and it is taking the advantage of calendar to show that particular date in StartDate field but the same calendar is not working for EndDate when we change from current date to any date greater than the date provided like 01/22/2016. Please guide me. Commented Jan 4, 2018 at 19:28
  • @Raj I didn't find any issue with your code.I have created a jsfiddle with the same. jsfiddle.net/Jibin/nh6eoj03/534 Commented Jan 5, 2018 at 4:57
  • @JibinBalachandran, Yes this fiddle is working as expected. But Please try to change EndDate manually, it is not taking an advantage of calendar. Commented Jan 5, 2018 at 19:27

2 Answers 2

1

If you want the dates to be changed manually(without the use of datepicker by editing the textbox), write an on change function and set the max value of datepickers accordingly.

Add the below functions to your js code.

$('#EndDate').on('change',function(){
    var selected=$('#EndDate').val();
    $("#StartDate").datepicker("option", "maxDate", selected)
});

 $('#StartDate').on('change',function(){
    var selected=$('#StartDate').val();
    $("#EndDate").datepicker("option", "minDate", selected)
});

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Thann you @Jibin. This is what I was looking for.
1

Hi i have created a similar demo which works. Are you writing JavaScript code in head section of page i think , just write that same code end of your view will work.

Model

public class DemoModel
{
    [Display(Name = "StartDate")]
    [Required(ErrorMessage = "Please Choose StartDate")]
    public DateTime? StartDate { get; set; }

    [Display(Name = "EndDate")]
    [Required(ErrorMessage = "Please Choose EndDate")]
    public DateTime? EndDate { get; set; }
}

View

@model DemoMVC.Models.DemoModel
    @{ Layout = null;}
@{
    ViewBag.Title = "Home Page";
}

<script src="~/Scripts/datetimepicker/jquery-1.12.4.js"></script>
<script src="~/Scripts/datetimepicker/jqueryui.js"></script>
<link href="~/Scripts/datetimepicker/jqueryui.css" rel="stylesheet" />

<div class="col-sm-6">
    <div class="form-group">
        @Html.LabelFor(m => m.StartDate)
        @Html.TextBoxFor(m => m.StartDate, new { @class = "form-control", placeholder = "Enter start date" })
        @Html.ValidationMessageFor(m => m.StartDate, "", new { @class = "error" })
    </div>
</div>

<div class="col-sm-6">
    <div class="form-group">
        @Html.LabelFor(m => m.EndDate)
        @Html.TextBoxFor(m => m.EndDate, new { @class = "form-control", placeholder = "Enter finish date" })
        @Html.ValidationMessageFor(m => m.EndDate, "", new { @class = "error" })
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#StartDate").datepicker({
            numberOfMonths: 1,
            onSelect: function (selected) {
                $("#EndDate").datepicker("option", "minDate", selected)
            }
        });
        $("#EndDate").datepicker({
            numberOfMonths: 1,
            onSelect: function (selected) {
                $("#StartDate").datepicker("option", "maxDate", selected)
            }
        });

    });

</script>

Output

output

5 Comments

Enddate is still not working properly as expected. First please change start date manually by writting any date like 01/16/2010 before current date and it is taking the advantage of calendar to show that particular date in StartDate field but the same calendar is not working for EndDate when we change from current date to any date greater than the date provided like 01/22/2016. Please provide me proper guidance. In normal circumstance, it is working perfectly fine. But the problem arises only when we change EndDate manually.
@raj on keypress or keydown event do not allow user to enter date manually. show alert please choose date. if it help you please mark this as answer.
Is there any way where I can create a dropdown calendar so that user can select month and year from it. I think this will be easy in spite of doing manual change in date.
@raj it is not change in datepicker just a validation on textbox where you will not allow user to enter date just choose from datepicker.
@raj it is not change in datepicker just a validation on textbox where you will not allow user to enter date just choose from datepicker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.