Text Field Validation within a date range

Community Beginner ,
Jun 07, 2022 Jun 07, 2022

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?

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

39

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

Yes, it is possible. 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

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;
	}
}

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thank you so much.  Exactly what I needed.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

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)

Likes

Translate

Translate

Report

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