locked
DateTime - Date Read-Only RRS feed

  • Question

  • I have two datetime fields on a form. One is the start and one the end. The date part must be the same for both and only the time varies. I have JavaScript code to automatically set the date and verify the restrictions. However, I would prefer that the user cannot make the mistake of setting different dates in the first place. So is there a way to disable (make read-only) the date part of a datetime field? Or even better not even show the date part. This is on CRM2011 online.
    Tuesday, January 31, 2012 3:37 PM

Answers

  • You can accomplish this with a little unsupported JavaScript.  Below is a function you can pass your attribute name into; I tested with scheduledend.  The code will disable the text box for the date part and the calendar date picker.  I commented out the lines to hide those elements, uncomment if you wish.  I might just disable the date part though, lest the users think the date is different, or if they set the date through Outlook, then it will be more difficult to troubleshoot.  As with all unspported customizations, use wisely and your mileage may vary.

    function DisableDateOnly(attrName) {
        var tabElem = document.getElementById(attrName);
        var tRow = tabElem.getElementsByTagName("tr")[0];
    
        var tDatas = tRow.getElementsByTagName("td");
        //uncomment these lines and remove the rest of the code to hide the date parts
        //tDatas[0].style.display = "none";
        //tDatas[1].style.display = "none";
    
        //the first tData has the date text box
        var tBoxes = tDatas[0].getElementsByTagName("input");
        if (tBoxes[0].id == "DateInput") {
            tBoxes[0].disabled = true;
        }
    
        var imgElem = document.getElementById(attrName + "img");
        imgElem.onclick = "";
        imgElem.disabled = true;
    
    }
    

    • Marked as answer by hfaun Tuesday, January 31, 2012 10:21 PM
    Tuesday, January 31, 2012 6:17 PM

All replies

  • You can accomplish this with a little unsupported JavaScript.  Below is a function you can pass your attribute name into; I tested with scheduledend.  The code will disable the text box for the date part and the calendar date picker.  I commented out the lines to hide those elements, uncomment if you wish.  I might just disable the date part though, lest the users think the date is different, or if they set the date through Outlook, then it will be more difficult to troubleshoot.  As with all unspported customizations, use wisely and your mileage may vary.

    function DisableDateOnly(attrName) {
        var tabElem = document.getElementById(attrName);
        var tRow = tabElem.getElementsByTagName("tr")[0];
    
        var tDatas = tRow.getElementsByTagName("td");
        //uncomment these lines and remove the rest of the code to hide the date parts
        //tDatas[0].style.display = "none";
        //tDatas[1].style.display = "none";
    
        //the first tData has the date text box
        var tBoxes = tDatas[0].getElementsByTagName("input");
        if (tBoxes[0].id == "DateInput") {
            tBoxes[0].disabled = true;
        }
    
        var imgElem = document.getElementById(attrName + "img");
        imgElem.onclick = "";
        imgElem.disabled = true;
    
    }
    

    • Marked as answer by hfaun Tuesday, January 31, 2012 10:21 PM
    Tuesday, January 31, 2012 6:17 PM
  • Richard, it works great and is exactly what I was looking for. Thanks.
    Tuesday, January 31, 2012 10:21 PM