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

  • Verified answer
    Charan Raju C R Profile Picture
    7 Moderator on at
    RE: Single line of text to Date field

    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. 

  • yify Profile Picture
    1,023 on at
    RE: Single line of text to Date field

    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
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Single line of text to Date field

    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.

  • Charan Raju C R Profile Picture
    7 Moderator on at
    RE: Single line of text to Date field

    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");
    }

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at
    RE: Single line of text to Date field

    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. 

  • yify Profile Picture
    1,023 on at
    RE: Single line of text to Date field

    Hi Andrew,

    I am in the latest version of the Dynamics.

    Thanks

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Single line of text to Date field

    Hello,

    What version are you on?

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans