Join the Team Forms community

Updated 4 months ago

Help with multiconditional JavaScript validation

At a glance

The community member is working on a use case that involves a start date, end date, and a goaled number value (must be greater than 0). If the goal value (quotaGoal) is left blank, no validation is needed. If the goal is >0, then the start date (startQuota) must be set to the 1st of the month selected by the user and the end date (endQuota) must be set to the last day of the month. The community member has provided validation statements to handle the 1st/last day of the month, but is stuck on how to connect them with the goaled number field.

Another community member provided an answer suggesting the following validation for the start date: if(data.quotaGoal > 0){ const date = moment(data.startQuota); const isFirstDayOfMonth = date.isSame(date.clone().startOf('month'), 'day'); valid = isFirstDayOfMonth }

In this use case, there is a start date, end date, and a goaled number value (must be greater than 0). if the goal value (quotaGoal) is left blank, no validation is needed. if the goal is >0, then the start date (startQuota) must be set to the 1st of the month selected by the user and the end date (endQuota) must be set to the last day of the month.


I have come up with the below validation statements to handle the 1st/last day of the month, but am stuck on how to connect them with value the goaled number field.


Start Date must be 1st of month

JavaScript
const date = moment(data.startQuota);
const isFirstDayOfMonth = date.isSame(date.clone().startOf('month'), 'day');
valid = isFirstDayOfMonth

End Date must be last date of month

JavaScript
const date = moment(data.endQuota);
const isLastDayOfMonth = date.isSame(date.clone().endOf('month'), 'day');
valid = isLastDayOfMonth


Thanks for any help!


Marked as solution

Hi @Dan ,

Based on the information you provided the custom validation for the "Start Date" should be something like the below. You may need to modify this in case I have misunderstood your request, however it should be a starting point.


JavaScript

if(data.quotaGoal > 0){
	const date = moment(data.startQuota);
	const isFirstDayOfMonth = date.isSame(date.clone().startOf('month'), 'day');
	valid = isFirstDayOfMonth
} 
View full solution
E
D
1 comment·1 reply

Hi @Dan ,

Based on the information you provided the custom validation for the "Start Date" should be something like the below. You may need to modify this in case I have misunderstood your request, however it should be a starting point.


JavaScript

if(data.quotaGoal > 0){
	const date = moment(data.startQuota);
	const isFirstDayOfMonth = date.isSame(date.clone().startOf('month'), 'day');
	valid = isFirstDayOfMonth
} 

this is exactly what I needed, thank you @Erin Dwyer