locked
Date validation (No past dates) on date type column on sharepoint 2010 custom list RRS feed

  • Question

  • I have created a new custom Newitem.aspx form in sharepoint designer and make it as default form for 'add new item'.

    I my list i have a date type column and if i select a past it should show me a Validation message .

    previously i was using this ListSettings/Validation settings/ and put a logic [date]>=Todays()

    but it works only for default newform page not for custom form.

    any suggestions???


    Friday, October 19, 2012 9:29 AM

Answers

  • SharePoint forms will call a Javascript function called "PreSaveAction" before submitting, even your custom one.  You can provide this function to perform custom validations.  Check this post for full details: http://replicrux.com/2011/11/09/validating-forms-in-sharepoint.aspx

    So, on your form you can have something like this:

    function PreSaveAction(){
       
        // Get the date from your form
        var userDate = document.getElementById('YOURDATEFIELDID');
    
        // Get today's date
        var today = new Date();
    
        // Check if the date is greater/equal to today
        if (userDate >= today) {
           return true;
        }
        else {
           // Populate an error field for user to see
           return false;
        }
    }


    Brandon Atkinson
    Blog: http://brandonatkinson.blogspot.com

    • Proposed as answer by Brandon Atkinson Friday, October 19, 2012 11:26 AM
    • Marked as answer by Qiao Wei Wednesday, October 31, 2012 10:25 AM
    Friday, October 19, 2012 11:25 AM
  • Why don't you add a validation rule in your custom form on that field if value in this field is smaller than current date then show your validation error message that will automatically prevent the user to submit the form if he/she will enter a past date.

    #RoHsTr

    • Marked as answer by Qiao Wei Wednesday, October 31, 2012 10:25 AM
    Sunday, October 21, 2012 10:29 AM

All replies

  • SharePoint forms will call a Javascript function called "PreSaveAction" before submitting, even your custom one.  You can provide this function to perform custom validations.  Check this post for full details: http://replicrux.com/2011/11/09/validating-forms-in-sharepoint.aspx

    So, on your form you can have something like this:

    function PreSaveAction(){
       
        // Get the date from your form
        var userDate = document.getElementById('YOURDATEFIELDID');
    
        // Get today's date
        var today = new Date();
    
        // Check if the date is greater/equal to today
        if (userDate >= today) {
           return true;
        }
        else {
           // Populate an error field for user to see
           return false;
        }
    }


    Brandon Atkinson
    Blog: http://brandonatkinson.blogspot.com

    • Proposed as answer by Brandon Atkinson Friday, October 19, 2012 11:26 AM
    • Marked as answer by Qiao Wei Wednesday, October 31, 2012 10:25 AM
    Friday, October 19, 2012 11:25 AM
  • Why don't you add a validation rule in your custom form on that field if value in this field is smaller than current date then show your validation error message that will automatically prevent the user to submit the form if he/she will enter a past date.

    #RoHsTr

    • Marked as answer by Qiao Wei Wednesday, October 31, 2012 10:25 AM
    Sunday, October 21, 2012 10:29 AM