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>


No comments:

Post a Comment