0

I need to limit the maximum date by today date, so I use the next code:

<div class="datepicker"> @Html.TextBoxFor(m => m.Birthdate, "{0:dd.MM.yyyy}", new { @class = "form-control", @style="max-width: 240px;" }) </div> <script type="text/javascript"> $(function () { $('.datepicker input').datepicker({ language: "ru", autoclose: true, format: "dd.mm.yyyy", todayHighlight: true, maxDate: new Date() }); }); </script> 

Due to some problems between different browsers I use a custom datevalidator also:

jQuery.validator.methods.date = function (value, element) { var pattern = new RegExp('^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$'); return this.optional(element) || pattern.test(value); } 

As I wrote in the title a restriction by min and max dates doesn't work (I tried to set a min date too). I don't sure but maybe it is due to a cutom validation code. But how do I need to change it then?
P.S. Im sorry if my question is too easy but I don't know much JS.

1 Answer 1

2

Using startDate and endDate you can set

$(function () { $('.datepicker input').datepicker({ language: "ru", autoclose: true, format: "dd.mm.yyyy", todayHighlight: true, startDate : new Date('2017-03-10'), endDate : new Date() }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" rel="stylesheet"/> <div class="datepicker"> <input type='text' id='Birthdate'/> </div>

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

1 Comment

Thank you very much. You saved my hours. Did they change properties names?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.