Jump to content
Sign in to follow this  
jeff73230

datepicker going wrong with .js and .aspx

Recommended Posts

Hi everyone,

On a new project, i would like to prevent users from entering a date earlier than the first day of the month preceding the current month.
To be more precise, today (dd/mm/yy -> 19/11/19) i would like to prevent entering a date earlier than 01/10/19 (dd/mm/yy format).

When using following code. It works.
 

<!DOCTYPE html>
<html>
<body>
<p>Click the button to display first day of preceding current month.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction() {
  var dateMin = new Date();
  dateMin.setFullYear(dateMin.getFullYear(), dateMin.getMonth() - 1, 1);
  document.getElementById("demo").innerHTML = dateMin;
}
</script>

</body>
</html>

When i click on Try it button, on 19th november 2019, i've got Tue Oct 01 2019 (+ time) displayed.

But when using following code in my .js file :

 

var dateMin = new Date();

dateMin.setFullYear(dateMin.getFullYear(), dateMin.getMonth() - 1, 1);
$(function () {
    $("input[InputTypeCheck='DateSaisieMin']").datepicker({
        onSelect: function () { },
        dateFormat: 'dd/mm/yy',
        minDate: dateMin,
        changeYear: true,
        changeMonth: true
    },
        $.datepicker.regional['fr']).attr('readonly', 'readonly');
});

and following code in my .aspx file :

<p>
	<label class="LblLibelle">Date</label>
	&nbsp;&nbsp;                                   
	<asp:TextBox ID="txt_dateDeclaration" runat="server" InputTypeCheck="DateSaisieMin" AutoPostBack="true"></asp:TextBox>
	<asp:RequiredFieldValidator ID="rfv_dateDeclaration" runat="server" ControlToValidate="txt_dateDeclaration"
		Display="Dynamic" ErrorMessage="La date de l'absence est obligatoire" ValidationGroup="ajout"> 
	<label class="zoneObligatoire">&nbsp;*</label> 
	</asp:RequiredFieldValidator>
</p>

I was expecting to be unable to select a date in the datepicker prior to 01/10/2019, but this is not the case. The datepicker shows every month since August. 

Can someone tell me where I made a mistake ?

Many Thanks to all.

 

Share this post


Link to post
Share on other sites

I think minDate expects a string with a specific format. You just passed in a Date object. You need to generate a string something like this:

minDate: dateMin.getDate() + "/" + dateMin.getMonth() + "/" + dateMin.getYear(),

If that doesn't work, it's probably because you need to pad the day and month with zeroes on the left.

Share this post


Link to post
Share on other sites

Hi Ingolme,

Thanks for reply... but it did not work.
I still have every month since August (even after emptying history, cache and erasing cookies).
Should not it work even without having to set the day and the month with zeros on the left ????? By taking today as an example, the day or the month are made of two digits (I totally agree that if I had taken November 2 or 3 as an example it would not have worked).

Do you have some lines of code that I could use to try to set the day and month with zeros on the left ?


Thanks anyway 😉

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...