Alert User When Selecting Disabled Date

Simflex 301 Reputation points
2023-05-02T20:17:55.9433333+00:00

Greetings experts,

Please forgive me for the tags I selected. I wanted jQuery but the tag would not show up for selection.

We have a requirement to prevent users from selecting from datepicker days longer than 60 days.

Users are also not allowed from selecting days greater than current date.

Days longer than 60 days are grayed out just as days greater than today's date are grayed out.

The script below shows that this works just fine.

However, when our users cannot select previous dates longer than 60 days and cannot select days greater than current date, they are confused as why they are unable to make these selections.

Our proposed solution is to alert a user when attempting to click on disabled dates.

Any ideas how to handle this?

Your assistance is greatly appreciated.

My current working script as far as date selection, is below:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
$('#datepicker').datepicker({ minDate: -60,
maxDate: 0
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,490 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
983 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,341 Reputation points Microsoft Vendor
    2023-05-03T03:30:06.2166667+00:00

    Hi @Simflex,

    You can change the jquery-ui library to make it easier.

    Disabled dates (those outside the min/max) get the class ui-state-disabled added to them by the datepicker. Bind the click event to that class and set an alert.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>jQuery UI Datepicker - Default functionality</title>
         <link rel="Stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
       <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
       
        <script type="text/javascript">
            $(function () {
              
                    $('#datepicker').datepicker({
                        minDate: -60,
                        maxDate: 0
                    });
               
                $(document).on('click', '.ui-state-disabled', function () {
                    alert('Invalid date!!!Only select dates within the previous 60 days!!!')
                })
    
            });
           
        </script>
    </head>
    <body>
        <p>Date: <input type="text" id="datepicker"/></p>
    </body>
    </html>
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.