Monday, October 15, 2012

Read Multiple lines of Text field value using JQuery


To read a value of multiple lines of text from a Edit Form or New Form. Assume Comments is the field name

<script  language="javascript" type="text/javascript" >
function PreSaveAction()
{
   var Comments = getTagFromIdentifierAndTitle("textarea","TextField","Comments");  

   var CommentsText = RTE_GetEditorDocument(Comments.id);
   var CommentsTextValue = CommentsText .body.innerText;
}

function getTagFromIdentifierAndTitle(tagName, identifier, title) 
{  
    var len = identifier.length;  
    var tags = document.getElementsByTagName(tagName);  
    for (var i=0; i < tags.length; i++)
    {  
        var tempString = tags[i].id;  
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
        {  
            return tags[i];  
        }  
    }  
    return null;  

</script>

Additionally, use the following code to read the selected value of a drop down.

var val =  $("select[title='ChoiceFieldName']").val();

Saturday, October 13, 2012

JavaScript date validation in SharePoint form using Designer

Requirement: To validate date fields in Edit form before submitting the form.

Conditions:

  1. Start Date should not be less than End Date
  2. Start Date and End Date should not be less than Today
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
Add the following code after above line

<script  language="javascript" type="text/javascript" >
function PreSaveAction()
{
var date1 = $("input[title='Planned Release Start Time']").val(); var date2 = $("input[title='Planned Release End Time']").val(); var today = new Date(); today.setHours(0,0,0,0); if( date1 != "" || date2 != "") { var arrDate1 = date1.split("/"); var useDate1 = new Date(arrDate1[2], arrDate1[0]-1,arrDate1[1] ); var arrDate2 = date2.split("/"); var useDate2 = new Date(arrDate2[2], arrDate2[0]-1,arrDate2[1]); if(useDate1 < today || useDate2 < today) { alert("Planned Release Start Date and End Date should not be earlier than Today"); return false; // Cancel the item save process } if(useDate1 > useDate2) { alert("The Planned Release End Date cannot be earlier than the Planned Release Start Date"); return false; // Cancel the item save process } }
 return true;

}
</script>