Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Restrict Past date

(0) ShareShare
ReportReport
Posted on by 365

I want to view and select  current and the future date

7115.pastedimage1608017494953v1.png

  • Guido Preite Profile Picture
    Guido Preite 54,069 Super User 2024 Season 1 on at
    RE: Restrict Past date

    a couple of corrections to Leah Ju code:

    1) this line

    var date1 = new Date(date);

    is not required, when you do a getValue against a datetime field, or is null (checked by the next line in that code) or it has a javascript Date object

    2) this line

    Xrm.Page.getAttribute("new_date").setValue(null);

    should use formContext, so formContext.getAttribute("new_date").setValue(null);

    3) this line

    alert("Sorry,You can't select past day.");

    alert works but you should use

    Xrm.Navigation.openAlertDialog({text: "Sorry,You can't select past day."});

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Restrict Past date

    Hi unbeatable,

    Unfortunately, you can’t restrict use select or view past date in the Date picker of the date field directly, there is no way to achieve it.

    However, you can use javascript to date field OnChange event to prevent use filling past date in the field.

    Just like following code:

    function pastDate(executionContext) {
        var formContext = executionContext.getFormContext();
        var today = new Date();
        var date = formContext.getAttribute('new_date').getValue();
        var date1 = new Date(date);
        if (date != null) {
            if (today > date1) {
                Xrm.Page.getAttribute("new_date").setValue(null);
                alert("Sorry,You can't select past day.");
            }
        }
    }

    Test Result:

    When I fill yesterday date, it will alert you.

    7840.pastedimage1608025204181v1.png

    when you click ok button of the dialog, the date field will be cleared.

    4478.pastedimage1608025309728v2.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,602 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,340 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans