Question:
Hi,
I have created a form on my website which retrieves user input into the wix database collection.
The form works well, except, now, I would like to add additional error validations.
The form collects the following
- deadline application date (date, dd/mm/yyyy)
- start date (date, dd/mm/yyyy)
- end date (date, dd/mm/yyyy)
I only know how to read a tiny bit of code but not write at all. So if someone could show me the code including beginning and end would be great to understand context!
I want to know how to achieve the following correctly please?
- when the user fills in the “Deadline Application Date”, the date cannot be in the past and the date must be at least 1 week from today’s date.
- when the user fills in the “Start Date”, the date cannot be in the past, the date must be at least 24 hours after the “Deadline Application Date”.
- when the user fills in the “End Date”, the date cannot be in the past, the date must be at least 24 hours after the “Start Date”.
4)How to have the code play out live anytime the input is changed? rather than on clicking the submit button?
Product:
[Which editor or feature is your question most relevant to? e.g. Wix Editor, Wix Studio Editor, Editor X.]
What are you trying to achieve:
[Explain the details of what you are trying to achieve. The more details you provide, the easier it is to understand what you need.]
What have you already tried:
I tried out the code below but no results when I preview
// Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
$w.onReady(function ()
{});
/**
* Adds an event handler that runs when an input element's value
is changed.
[Read more](https://www.wix.com/corvid/reference/$w.ValueMixin.html#onChange)
* @param {$w.Event} event
*/
export function deadlineApplicationDate_change(event) {
var deadlineDate = new Date(FormData.call($w('#deadlineApplicationDate')).value);
var startDate = new Date(FormData.call($w('#startDate')).value);
var endDate = new Date(FormData.call($w('#endDate')).value);
// Validate Deadline Application Date
var today = new Date();
var oneWeekFromToday = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
$w('#deadlineApplicationDate').onCustomValidation((value, reject) => {
if (deadlineDate < today || deadlineDate < oneWeekFromToday) {
reject('Deadline must be in the future and at least 1 week from today.');
$w('#textRejectDeadlineDt').text = 'Deadline must be in the future and at least 1 week from today.';
$w('#textRejectDeadlineDt').show();
} else {
$w('#textrejectstartdt').hide();
}
return;
}
// Validate Start Date
var minStartDate = new Date(deadlineDate.getTime() + 24 * 60 * 60 * 1000); $w('#startDate').onCustomValidation((value, reject) => {
if (startDate < minStartDate || startDate < today) {
reject('Start date must be at least 24 hours after the deadline and in the future.');
$w('#textrejectstartdt').text = 'Start date must be at least 24 hours after the deadline and in the future.';
$w('#textrejectstartdt').show();
} else {
$w('#textrejectstartdt').hide();
}
return;
}
// Validate End Date
var minEndDate = new Date(startDate.getTime() + 24 * 60 * 60 * 1000); $w('#endDate').onCustomValidation((value, reject) => {
if (endDate < minEndDate || endDate < today) {
reject('End date must be at least 24 hours after the start date and in the future.');
$w('#textRejectEndDt').text = 'End date must be at least 24 hours after the start date and in the future.';
$w('#textRejectEndDt').show();
} else {
$w('#textRejectEndDt').hide();
}
return;
});
// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
// Add your code for this event here:
Additional information:
I made sure the error validation texts in red are hidden by default, and i’ve tested without it being hidden on default, neither made a difference.
Thank you in advance for any guidance!