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

Javascript to format date field (field type is text)

(0) ShareShare
ReportReport
Posted on by 255

Hi ,

I have "Anniversary_Year_Month" field on form having field type as text field.

want to make validation like it must follow format "YYYY-MM", otherwise prevent save and show error message window.

can i do this in CRM 2011 using javascript.

I have the same question (0)
  • Verified answer
    Kipetcoff Profile Picture
    1,037 on at

    Hi,

    Here is the topic about this

    https://social.microsoft.com/Forums/en-US/7be7e082-7e86-4db5-97d6-da48d5f62601/dynamics-crm-2011-uk-phone-number-javascript-validation?forum=crmdevelopment

    Try this function

    function validateDate(context) {
        var txtValue = context.getEventSource().getValue();
        dateRegex = /([12]\d{3}-(0[1-9]|1[0-2]))/;
    
        if (!txtValue.match(dateRegex)) {
            {
                context.getEventSource().setValue('');
                event.returnValue = false;
                alert("The date format is incorrect");
            } 
        }
    }

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    Hi Mona,

    This is the below Javascript which you call on Onchange event of "Anniversary_Year_Month" field . It give you exact match of "YYYY-MM" 

    function startDateFormat(){
          var regs=[];
          var value = Xrm.Page.getAttribute("new_startdatetext").getValue();
           re = /^\d{4}\-\d{1,2}$/;
           if(value != ""){
               if(regs = value.match(re)){
                     if(regs[2] < 1 || regs[2] > 12){
                         alert("Invalid value for month ");
                     }
                     if(!isNaN(regs[1])){
                         alert("Invalid value of Year");
                     }
               }else{
                    alert("not in a right format (YYYY-MM)")
               }

           }

    }

  • Mona Chavan Profile Picture
    255 on at

    Thanks for the reply.

    Do i need to call this function directly onChange event of the field?

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    Yes, Change field name in  that line 

    Xrm.Page.getAttribute("new_startdatetext").getValue(); 

    and call this function on OnChnage

    Go to settings->Customization->Compnent->Entities-> 

    Go to that entity where this field is created and then go to forms->Form Properties (see below Image as I am using Updated version might you don't have same UI ) but you will get option.

    pastedimage1571380621787v2.png

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    on Save? it will call everytime even it is auto save or you have done any other custom code which make that record update or save.

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 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans