PLEASE HELP – custom question answer type date
-
Hello,
Could someone please help me out? I added a custom question (experience date) that needs to be answered with a valid date instead of text or number (possibilities now-on site review form). The question is required and the date can’t be older than 12 months or in the future. I tried different codes and none is the perfect one. Custom script at the moment:
jQuery(document).ready(function($) {
$('input[id^="cr_onsite_"][type="text"]').attr('type', 'date');
var today = new Date();
var oneYearAgo = new Date(today);
oneYearAgo.setFullYear(today.getFullYear() - 1);
$('input[id^="cr_onsite_"][type="date"]').each(function() {
$(this).attr('min', oneYearAgo.toISOString().split('T')[0]);
$(this).attr('max', today.toISOString().split('T')[0]);
$(this).attr('required', true);
});
$('#cr-review-form').on('submit', function(event) {
var isValid = true;
$('input[id^="cr_onsite_"][type="date"]').each(function() {
var dateInput = $(this).val();
var inputDate = new Date(dateInput);
if (!dateInput) {
isValid = false;
alert('Dit veld mag niet leeg zijn.');
$(this).focus();
return false;
}
if (inputDate.toString() === 'Invalid Date') {
isValid = false;
alert('Voer een geldige datum in.');
$(this).focus();
return false;
}
if (inputDate < oneYearAgo || inputDate > today) {
isValid = false;
alert('De datum moet binnen het afgelopen jaar liggen.');
$(this).focus();
return false;
}
});
if (!isValid) {
event.preventDefault();
}
});
});The field can still be send in empty or with a invalid date like 31-09-2024. Is it possible to make this field required again by filling in a date (instead of number or text)?
The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.