Troubleshooting problem with Ajax Calendar Extender

I have a text box where I need to save the chosen date from a calendar. To achieve this, I am using an AJAX calendar extender and setting the target control property to the textbox ID. However, when I click on a button on the same page, I lose the selected date (meaning I lose the text value), but I want to retain the selected date after clicking the button.

<td>
     <asp:TextBox ID="txtDate" runat="server" ReadOnly="true"></asp:TextBox>
</td>
 <td>
     <asp:ImageButton ID="imgCalender" runat="server" ImageUrl="~/Images/Calendar.png" ToolTip="Select a Date" />
   <asp:CalendarExtender ID="calShow"  runat="server" PopupButtonID="imgCalender" PopupPosition="BottomLeft" TargetControlID="txtDate" OnClientDateSelectionChanged="CheckForPastDate"></asp:CalendarExtender>
  </td>

Additionally, I would like it so that if a user tries to select a date more than 20 days ahead of the current date, the text box should be cleared, requiring the user to select a proper date again.

Javascript

var selectedDate = new Date();
selectedDate = sender._selectedDate;
var todayDate = new Date();

if (selectedDate.getDateOnly() <= todayDate.getDateOnly()) {
    alert('Date Cannot be in the past or current date');
    sender._textbox.set_Value(null);
} else if(selectedDate.getDateOnly() > todayDate.getDateOnly()) {
     // This placeholder indicates room for further code implementation
}

Answer №1

When working within a JavaScript method, you can implement the following logic:

var today = new Date();
var twentyDays = new Date();
twentyDays.setDate(today.getDate()+20);

if (selectedDate.getDateOnly() <= todayDate.getDateOnly()) 
{
    alert('Date Cannot be in the past or current date'); 
    sender._textbox.set_Value(null); 
} 
else if(selectedDate.getDateOnly() > twentyDays.getDateOnly()) 
{
    alert('this is more than 20 days');
    sender._textbox.set_Value(null); 
    return;
}  

Update 02/12/2013

If unsure about what the getDateOnly() method does and encountering issues, the script below can be used to test scenarios by adjusting the daysModifier variable.

var daysModifier = 23;
var selectedDate = new Date();
selectedDate.setDate(selectedDate.getDate() + daysModifier);

var today = new Date();
var twentyDays = new Date();
twentyDays.setDate(today.getDate()+20);

document.write(today);
document.write("<br />");
document.write(selectedDate);
document.write("<br />");
document.write(twentyDays);


if (selectedDate <= today) 
{
    alert('Date cannot be in the past or current date'); 
} 
else if(selectedDate > twentyDays) 
{
    alert('this is more than 20 days');
}  

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered a surprise hiccup while attempting to upload file to AWS S3 using browser (putObject

After successfully obtaining a presigned URL through my Node/Express backend for a putObject request on S3, I attempt to upload the relevant files via the browser. However, when making the put request through the browser (or Postman), I encounter a 400 or ...

What's the reason behind the automatic doubling of written files in C# programming?

Recently, I've been encountering a strange issue while writing results to a text file. The output seems to be duplicated, and I'm not sure why or when this started happening. I attempted the following: //test how many time running the file writi ...

What are some ways to incorporate JQuery with getElementById?

i am trying to retrieve all input elements within a form using JavaScript formEditable = document.getElementById("formid").getElementsByTagName("input"); However, I would like to achieve the same result using jQuery instead formEditable = $("#formid").f ...

NPM encountered difficulties in resolving the dependency tree

I seem to be encountering a persistent issue that I cannot resolve on my own. My attempt to update webpack and webpack-cli in a project has been met with repeated errors of this nature: npm install webpack@latest --save-dev npm ERR! code ERESOLVE npm E ...

"Encountered a TypeError: Cannot read property 'params

I've encountered an issue with passing the id to my product page. Despite trying various solutions and searching for answers, I still can't get it to work. Below is my index.js code: import React from "react"; import {render} from &quo ...

Execute Javascript after modification of the DOM

I have developed two custom directives known as app-content and app-content-item. These directives are intended to be utilized in upcoming projects to provide a basic structure with simple styling. They will be incorporated into a module and should be nest ...

Handling a jQuery Ajax success callback when it fails

I am encountering an issue with my ajax post request where even though I have separate success, failure, and status code handlers, when the request fails with a 400 error, part of the success function is still being executed. Can someone provide insight in ...

A guide on distinguishing between two dates in Ionic3 using either date-fns or Moment libraries

How can I calculate the difference between two dates to determine the end of an employee's service? Similar Question: What is the best way to find the day difference between two dates using Ionic 3? I am looking for a solution on how to get the exac ...

How can I change the orientation of a cube using d3js?

Seeking guidance on creating an accurate chart using d3js How can I rotate the SVG to display the opposite angle as shown in the image? Any recommended resources for achieving this desired result would be greatly appreciated. The provided code only disp ...

Controlling the maximum number of components displayed on each row in a loop or map using React

I'm having some trouble with this seemingly simple task and could use some guidance. Any suggestions would be greatly appreciated. Thank you. My Current Situation Currently, I am working with React.js and have an array containing 20 elements. What ...

initiate a click event within another click event

This is an HTML Code snippet: <ul id="linkjess"> <li><a href="javascript:setfolder('medewerkers', '1000');">medewerkers 1000</a></li> <li><a href="javascript:setfolder('medewerkers', ...

Changes to the className of a React component will trigger a re-render of

When the className of the parent changes, React children will re-render. import React from 'react'; import { useSelector } from 'react-redux'; import items from './ItemsList.js'; import Item from './Item'; import &ap ...

Updating HTML Pages with Dynamic Content

Dealing with a massive project consisting of 50,000 pages (20,000 aspx forms, 10,000 asp forms, and 10,000 html pages) can be overwhelming. With only 2 days to complete the task of adding content after the body tag on all pages, I am seeking advice on ho ...

The GitHub-hosted package encounters a failure during the npm publish process

Last week everything was running smoothly, but now I am encountering an error and not sure what went wrong. (Apologies for the formatting as there are many lines of log) Running Node version: v10.15.3 Using npm version: 6.4.1 After executing npm publish ...

Is there a way to make $animate.removeClass function within a directive without needing to use $eval

I have developed a custom directive that smoothly fades out and fades in the content whenever there is a change in the model. app.controller('myCtrl', function($scope, $interval) { $scope.number = 0; $interval(function() { $scope.number+ ...

Leveraging the Spread Operator in Redux mapDispatchToProps with SweetAlert2

When I add the swal action creators to the mapDispatchToProps function like this: function mapDispatchToProps(dispatch) { return { getAnimal: (_id) => dispatch(getAnimal(_id)), ...swal } } The issue aris ...

Is it advisable to implement try/catch blocks in the lower levels of my WebApi application?

In my WebApi application for user storage, I have implemented DAL and BLL layers. The ExceptionFilters are used for handling exceptions. public class HandleExceptionsAttribute : ExceptionFilterAttribute { private readonly ILogger logger; ...

The consistency of values remains constant throughout all Ajax requests

I am presenting the code snippet below: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" ...

Ensuring promise doesn't resolve until the IF STATEMENT is executed

I am encountering an issue with the "checkWorkflow" function where it seems to be executing the "If" statement before actually checking. This deduction is based on the output in my console, which makes me believe there might be a problem with how I am hand ...

Utilize PHP in an APP Engine Application to send an email to a Gmail address

I have a project deployed on Google App Engine where I need to send an email once a user submits the contact form. My app is successfully deployed and I have implemented an ajax request to a PHP file, but unfortunately, it's not functioning as expect ...