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
-
davidnarramoreMemberMember for: 8 years 8 months
[#10]: Thank you so much Dimitry. This worked great! When would be the best time to do this validation? On submit? would that be fd.onsubmit( );
-
davidnarramoreMemberMember for: 8 years 8 months
[#10]: Thank you so much Dimitry. This worked great! When would be the best time to do this validation? On submit? would that be fd.onsubmit( );
-
Dmitry KozlovAdminMember for: 10 years 1 day
[#12]: Hi,
Yes, if you need to validate the fields before the submission, just put this code into onsubmit handler:
function getHours(str) { var parts = str.split(' '); if (parts[1] == 'PM') { return parseInt(parts[0]) + 12 } parseInt(parts[0]) } fd.onsubmit(function() { 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]); if (endDate < startDate) { alert('End Date must be greater than Start Date.'); return false; } return true; });
-
davidnarramoreMemberMember for: 8 years 8 months
[#13]: When I put the code in the alert displays as expected. When I change the time so the endDate is greater than the startDate the alert continues to display.
-
Dmitry KozlovAdminMember for: 10 years 1 day
[#14]: Please, try to trace variables before the comparison. Also, ensure that you don't have JS-errors in the browser console.
alert(endDate); alert(startDate); if (endDate < startDate) { alert('End Date must be greater than Start Date.'); return false; }
-
SpongeBen SquareJawMemberMember for: 8 years 11 months 3 days
[#15]: You need to fix the gethours function for this to work put in ; and retun
function getHours(str) {
var parts = str.split(' ');
if (parts[1] == 'PM') {
return parseInt(parts[0]) + 12;
}
return parseInt(parts[0]);
}
-
Dmitry KozlovAdminMember for: 10 years 1 day
[#16]: Exactly! Many thanks.
-
SpongeBen SquareJawMemberMember for: 8 years 11 months 3 days
[#17]: Actually, I have a correction to my correction. It seems if it is 12 PM and if you add 12 the value would be
24 so it would add a day. This should be solid now. For DateTime Comparison
function getHours(str) {
var parts = str.split(' ');
var partHour = 0;
if (parts[1] == 'PM') {
partHour = parseInt(parts[0])
if (partHour != 12){
return parseInt(parts[0]) + 12;
}
}
return parseInt(parts[0]);
}
-
KatyMemberMember for: 9 years 2 months 7 days
I am checking if the date and time field is empty, but can't make it work, here is what i have after reading the above:
function getHours(str) {
var parts = str.split(' ');
if (parts[1] == 'PM') {
return parseInt(parts[0]) + 12;
}
return parseInt(parts[0]);
}
fd.onsubmit (function () {
var dt = fd.field('DueDate').value()
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('Fill out date');
}
return false;
}
return true;
});
-
YuriyMedvedevModeratorMember for: 6 years 11 months 27 days
[#19]: Hi Katy! Try this one, please:
fd.onsubmit (function () { var dt = fd.field('DueDate').value() if (Boolean(dt[0])) { alert('Date is filled!'); return true; } else{ alert('Fill the date, please'); return false;} });