Text Field Validation within a date range
Copy link to clipboard
Copied
Is it possibly in a Text Field with date format, if someone enters a date outisde of a range you specify to pop up an error message?
Copy link to clipboard
Copied
Yes, it is possible.
Copy link to clipboard
Copied
Here's an example of a custom validation script that does it:
var startDateString = "01/01/2022";
var endDateString = "07/01/2022";
var startDate = util.scand("mm/dd/yyyy", startDateString);
var endDate = util.scand("mm/dd/yyyy", endDateString);
if (event.value) {
var d = util.scand("mm/dd/yyyy", event.value);
if (d.getTime()<startDate.getTime() || d.getTime()>endDate.getTime()) {
app.alert("Error! The entered date must be between " + startDateString + " and " + endDateString);
event.rc = false;
}
}
Copy link to clipboard
Copied
Thank you so much. Exactly what I needed.
Copy link to clipboard
Copied
You can turn both date to seconds and then compare them, something like this:
var date1 = util.scand("mm/dd/yyyy", event.value);
var date2 = util.scand("mm/dd/yyyy", "07/01/2022");
var sec1 = date1.getTime() / 1000;
var sec2 = date2.getTime() / 1000;
if(event.value && sec1 > sec2){
app.alert("Message goes here");
event.rc = false;}
Use it as validation script of date field.
Set which date you want in variable date2 (I used 07/01/2022)

