Date field validation
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 KozlovAdminMember for: 9 years 8 months 16 days
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?
-
coresoulMemberMember for: 8 years 3 months 13 days
[#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 KozlovAdminMember for: 9 years 8 months 16 days
[#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.
-
davidnarramoreMemberMember for: 8 years 4 months 16 days
[#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();
}
}); -
coresoulMemberMember for: 8 years 3 months 13 days
[#3]: It didnot work for me either.
-
Dmitry KozlovAdminMember for: 9 years 8 months 16 days
[#4]: David,
Could you drop me HTML-source of your form page?
-
Dmitry KozlovAdminMember for: 9 years 8 months 16 days
[#6]: Please, send it to [email protected]
-
Dmitry KozlovAdminMember for: 9 years 8 months 16 days
[#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); }
-
davidnarramoreMemberMember for: 8 years 4 months 16 days
[#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 KozlovAdminMember for: 9 years 8 months 16 days
[#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);