none
End date must be greater than start date in sharepoint list? RRS feed

  • Question

  • Hi all,

    I created a custom list in sharepoint(wss3.0) with two columns 'start' and 'end' as DateTime type. Now we want that 'end' must be greater than 'start' by using jquery. How we can acheive this?


    Thanks & Regards:

    Pradeep Gangwar


    Friday, February 17, 2012 5:53 PM

Answers

  • Hi all,

    I used this javascript to acheive this:

    <script language="javascript" type="text/javascript">
    function PreSaveAction() {
        var date1 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","start");
        var date2 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","end");
        var arrDate1 = date1.value.split("/");
        var useDate1 = new Date(arrDate1[2], arrDate1[0]-1, arrDate1[1]);
        var arrDate2 = date2.value.split("/");
        var useDate2 = new Date(arrDate2[2], arrDate2[0]-1, arrDate2[1]);
        if(useDate1 > useDate2)
        {
            alert("The End Date cannot happen earlier than the Start Date");
            return false; 
        }
        return true; 
    }
    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>
    This code works fine, but I want to use jquery for this. And this works on only Date, how on Date & Time both simultaneously.

    Thanks & Regards Pradeep Gangwar

    Friday, February 17, 2012 6:03 PM

All replies

  • Hi all,

    I used this javascript to acheive this:

    <script language="javascript" type="text/javascript">
    function PreSaveAction() {
        var date1 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","start");
        var date2 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","end");
        var arrDate1 = date1.value.split("/");
        var useDate1 = new Date(arrDate1[2], arrDate1[0]-1, arrDate1[1]);
        var arrDate2 = date2.value.split("/");
        var useDate2 = new Date(arrDate2[2], arrDate2[0]-1, arrDate2[1]);
        if(useDate1 > useDate2)
        {
            alert("The End Date cannot happen earlier than the Start Date");
            return false; 
        }
        return true; 
    }
    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>
    This code works fine, but I want to use jquery for this. And this works on only Date, how on Date & Time both simultaneously.

    Thanks & Regards Pradeep Gangwar

    Friday, February 17, 2012 6:03 PM
  • Why jquery?

    Why not use the built-in validation function i wss?

    Check the list settings in your wss list - and in particular 'Validation Settings'.

    /Lars Hammarberg


    //Lars Hammarberg www.camako.se Gold Certified Partner

    Monday, February 20, 2012 10:35 PM
  • To Lars Hammarberg

    Actually you are right..but we are talking about sharepoint 2007 site. So, please suggest the solution related to 2007 site.

    Tuesday, February 21, 2012 1:06 PM