I have a form with two date selectors, start and end date. I have a hidden field that contains a user meta generated date. i would like to block submission and generate an error message if either of the dates is on or later than the user meta generated date. I have a script that i have been trying but with no success. As an FYI I have also had minimal success with user meta content in fields. Is this possible?
jQuery(document).ready(function($) {
// The CSS classes of the two date fields
var dateField1 = $('#field_cxq7o');
var dateField2 = $('#field_fo9mi');
// The field key of the user meta date field
var blockedDateField = $('.frm_field[data-key="la1dv"]');
// Handler to the form's submit event
$('.frm_form').on('submit', function(e) {
// Get the selected dates from both fields
var selectedDate1 = new Date(dateField1.val());
var selectedDate2 = new Date(dateField2.val());
// Get the blocked date
var blockedDate = new Date(blockedDateField.val());
// Perform the date comparisons and block submission if necessary
if (selectedDate1 >= blockedDate || selectedDate2 >= blockedDate) {
e.preventDefault(); // Prevent form submission
alert('Submission blocked: one of the selected dates is on or after the restricted date.'); // Error message
}
});
});
Next, why are you creating field objects instead of just getting the values? Your first two lines should be:
var dateField1 = $('#field_cxq7o').val(),
dateField2 = $('#field_fo9mi').val(),
blockedDateField = $('#field_la1dv').val();
You don't need any of the code instantiating new data fields.
Last, you should not be using the submit event to validate a field. You should be using https://formidableforms.com/knowledgebase/frm_validate_field_entry/ as the hook.
Please login or Register to submit your answer