Date field validation
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
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?
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?
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;
}
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
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Please, find samples below:
As for the formatting, it's checking automatically by the SharePoint field.
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()) {
...
}
-
- Posts: 76
- Joined: Wed Jan 21, 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();
}
});
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();
}
});
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
David,
Could you drop me HTML-source of your form page?
Could you drop me HTML-source of your form page?
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Please, send it to support@spform.com
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
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);
}
-
- Posts: 76
- Joined: Wed Jan 21, 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?
-
- Information
-
Who is online
Users browsing this forum: No registered users and 2 guests