web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Single line of text to Date field

(0) ShareShare
ReportReport
Posted on by 1,023

Hi,

I have a single line of text field and want to use it for the purpose of storing the date value.  Just wondering if there are easy options to accomplish this?

 I want  the field to accept the date format as mm/dd/yyyy 

If JS is only way to accomplish this, any references or JS code is highly appreciated.

Please advise.

Thanks

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    What version are you on?

  • yify Profile Picture
    1,023 on at

    Hi Andrew,

    I am in the latest version of the Dynamics.

    Thanks

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi James,

    Please refer to the code below: 

    function textAsDateValidation(executionContext){
        var formContext = executionContext.getFormContext();
        var dateRegex = /^((0?[1-9]|1[012])[/](0?[1-9]|[12][0-9]|3[01])[/](19|20)?[0-9]{2})*$/;
        var preventSaveAndDisplayNotification = (formContext) => {
            formContext.getControl("mea_textasdate").addNotification({
                messages: ['Message to Display...(MM/DD/YYYY)'],
                notificationLevel: 'ERROR',
                uniqueId: 'uniqueId',
            });
        }
        !dateRegex.test(formContext.getControl('mea_textasdate').getValue()) ? preventSaveAndDisplayNotification(formContext) : formContext.getControl('mea_textasdate').clearNotification('uniqueId');
    }

    Then register your function at the change of your field. I used mea_textasdate as an example. 

  • Charan Raju C R Profile Picture
    7 Moderator on at

    Hi James,

    You can use below javascript to validate the date value in text field. Also you can restrict the text field length to 10.

    You may also need to implement same validation at server side by registering plug-in steps on create of record and on update of text this field to avoid setting invalid date value from other sources like data import and integration.

    function isValidDate(dateString) {
        if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
            return false;
    
        var parts = dateString.split("/");
        var day = parseInt(parts[1], 10);
        var month = parseInt(parts[0], 10);
        var year = parseInt(parts[2], 10);
    
        if(year < 1000 || year > 3000 || month == 0 || month > 12)
            return false;
    
        var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
    
        if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
            monthLength[1] = 29;
    
        return day > 0 && day <= monthLength[month - 1];
    }
    
    function onDateFieldChange() {
        var dateString = Xrm.Page.getAttribute("TextFieldName").getValue();
        if(dateString != null && !isValidDate(dateString))
            Xrm.Page.getControl("TextFieldName").setNotification("Please enter valid date with MM/DD/YYYY format.","100");
        else 
            Xrm.Page.getControl("TextFieldName").clearNotification("100");
    }

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Then potentially you can use Masked Text Control - docs.microsoft.com/.../additional-controls-for-dynamics-365-for-phones-and-tablets. Obviously, you will have to do some validation that data is valid. You can use one of approaches suggested in the post.

  • yify Profile Picture
    1,023 on at

    Hi Charan,

    Thanks for the code. I verified the code works in all the cases but just having a minor issue. I have this setup on the field change. After I add the wrong format of date it throws an error which is expected but when I clear the date completely in the field, the error message doesnt seem to go away. Please  advise on how to fix it.

    Thanks

  • Verified answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    Hey James, I have just updated my javascript code to add null check as below. Please update it at your end as well.

    dateString != null

    Please mark the post as a suggested answer if it is helpful to resolve your problem. 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans