Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Text Field Validation within a date range

Community Beginner ,
Jun 07, 2022 Jun 07, 2022

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?

TOPICS
Create PDFs , How to , JavaScript , PDF forms
754
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 07, 2022 Jun 07, 2022

Yes, it is possible. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 07, 2022 Jun 07, 2022

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;
	}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 07, 2022 Jun 07, 2022
LATEST

Thank you so much.  Exactly what I needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 07, 2022 Jun 07, 2022

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines