Date field validation

rss

Posted by coresoul - Apr 20 ’15 at 03:52

Hi, how do i validate a date field for following:

  • empty validation
  • date greater than today
  • date format validation

Thanks

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 10 months 26 days
    #1 by Dmitry Kozlov Apr 21 ’15 at 06:01

    Hi,

    You can make a field mandatory in the column settings and specify validation rule in the list settings: List settings -> Validation settings. Or you mean that you need to do it dynamically?

  • coresoul
    Member
    Member for: 4 years 5 months 22 days
    #2 by coresoul Apr 21 ’15 at 08:13

    [#1]: Since i am doing all my validation via js in your form builder, i would like to validate in form builder. Please let me know how can i do it. here are my other validation.

    fd.onsubmit(function() {

    if(!fd.field('Title').value().trim()) {

    alert('Please, fill in the Position field.');

    $('.position input').focus();

    return false;

    }

    //else if(!fd.field('DateNeeded').value()) {

    //alert('Please, fill in the date needed field.');

    //$('.dateneeded input').focus();

    //return false;

    //}

    else if (!fd.field('AccountNo').value().trim()) {

    alert('Please, fill in the Account No. field.');

    $('.accountno input').focus();

    return false;

    }

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 10 months 26 days
    #3 by Dmitry Kozlov Apr 22 ’15 at 08:10

    [#2]: Please, find samples below:

    // checking whether a date field is empty
    if (!Boolean(fd.field('Date').value())) {
    	// field is empty
    }
    
    // getting date
    var dateParts = fd.field('Date').value().split('/');
    var date = new Date(dateParts[2], dateParts[0]-1, dateParts[1]);
    
    // checking whether it is greater than today
    if (date > new Date()) {
    	...
    }
    

    As for the formatting, it's checking automatically by the SharePoint field.

  • davidnarramore
    Member
    Member for: 4 years 6 months 25 days
    #4 by davidnarramore Apr 23 ’15 at 09:28

    [#3]: I used your example and created a function to check my startDate and endDate (internal names) fields to compare them and alert if the startDate was greater than the endDate but it doesn't work. Do you see what my problem is?

    fd.onsubmit(function checkDates {
    var dateParts = fd.field('startDate').value().split('/');
    var startDate = new Date(dateParts[2], dateParts[0]-1, dateParts[1]);
    var dateParts2 = fd.field('endDate').value().split('/');
    var endDate = new Date(dateParts2[2], dateParts2[0]-1, dateParts2[1]);

    if (startDate > endDate {
    alert('The End Date is before the Start Date.');
    $('.Start Date').focus();
    }
    });

  • coresoul
    Member
    Member for: 4 years 5 months 22 days
    #5 by coresoul Apr 23 ’15 at 09:31

    [#3]: It didnot work for me either.

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 10 months 26 days
    #6 by Dmitry Kozlov Apr 23 ’15 at 10:48

    [#4]: David,

    Could you drop me HTML-source of your form page?

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 10 months 26 days
    #7 by Dmitry Kozlov Apr 23 ’15 at 10:48

    [#6]: Please, send it to [email protected]

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 10 months 26 days
    #8 by Dmitry Kozlov Apr 24 ’15 at 04:40

    [#5]: If you use a date with time field, please, try the following code to get its value:

    function getHours(str) {
    	var parts = str.split(' ');
    	if (parts[1] == 'PM') {
    		return parseInt(parts[0]) + 12
    	}
    	
    	parseInt(parts[0])
    }
    
    var dt = fd.field('DateTime').value()
    // check whether the field is empty
    if (Boolean(dt[0])) {
    	var dateParts = dt[0].split('/');
    	var date = new Date(dateParts[2], dateParts[0]-1, dateParts[1], getHours(dt[1]), dt[2]);
    	alert(date);
    }
    
  • davidnarramore
    Member
    Member for: 4 years 6 months 25 days
    #9 by davidnarramore Apr 24 ’15 at 07:57

    [#8]: I'm sorry for the confusion, but I'm trying to compare two date time fields (startDate and endDate) which can span multiple days: startDate might be 4/24/2015 2pm and the endDate might be 4/26/2015 11am. I was hoping to do something like getTime() and compare to see if the startDate is greater than endDate. The user may have selected the wrong day by accident. Doesn't the code above just check the hours?

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 10 months 26 days
    #10 by Dmitry Kozlov Apr 27 ’15 at 09:38

    [#9]:Hi,

    The code from my previous message retrives a date-object based on both date and time. So you can use it to compare two date and time fields:

    function getHours(str) {
    	var parts = str.split(' ');
    	if (parts[1] == 'PM') {
    		return parseInt(parts[0]) + 12
    	}
    	
    	parseInt(parts[0])
    }
    
    var dt = fd.field('startDate').value();
    var dateParts = dt[0].split('/');
    var startDate = new Date(dateParts[2], dateParts[0]-1, dateParts[1], getHours(dt[1]), dt[2]);
    
    dt = fd.field('endDate').value();
    dateParts = dt[0].split('/');
    var endDate = new Date(dateParts[2], dateParts[0]-1, dateParts[1], getHours(dt[1]), dt[2]);
    
    alert(endDate > startDate);
    
Displaying 1 to 10 of 28 messages
Previous123