Page 1 of 3
Date field validation
Posted: 20 Apr 2015
by coresoul
Hi, how do i validate a date field for following:
empty validation
date greater than today
date format validation
Thanks
Re: Date field validation
Posted: 21 Apr 2015
by Dmitry Kozlov
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?
Re: Date field validation
Posted: 21 Apr 2015
by coresoul
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;
}
Re: Date field validation
Posted: 22 Apr 2015
by Dmitry Kozlov
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.
Re: Date field validation
Posted: 23 Apr 2015
by davidnarramore
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();
}
});
Re: Date field validation
Posted: 23 Apr 2015
by coresoul
It didnot work for me either.
Re: Date field validation
Posted: 23 Apr 2015
by Dmitry Kozlov
David,
Could you drop me HTML-source of your form page?
Re: Date field validation
Posted: 23 Apr 2015
by Dmitry Kozlov
Please, send it to
support@spform.com
Re: Date field validation
Posted: 24 Apr 2015
by Dmitry Kozlov
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);
}
Re: Date field validation
Posted: 24 Apr 2015
by davidnarramore
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?