Date field validation

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
coresoul
Posts: 23
Joined: Tue Feb 24, 2015

20 Apr 2015

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

empty validation
date greater than today
date format validation

Thanks

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

21 Apr 2015

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
Posts: 23
Joined: Tue Feb 24, 2015

21 Apr 2015

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;

}

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

22 Apr 2015

Please, find samples below:

Code: Select all

// 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
Posts: 76
Joined: Wed Jan 21, 2015

23 Apr 2015

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
Posts: 23
Joined: Tue Feb 24, 2015

23 Apr 2015

It didnot work for me either.

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

23 Apr 2015

David,

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

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

23 Apr 2015

Please, send it to support@spform.com

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

24 Apr 2015

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

Code: Select all

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
Posts: 76
Joined: Wed Jan 21, 2015

24 Apr 2015

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?

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests