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

how to validate field date range, despite allowable formats of m/d/yyyy and just yyyy.

Community Beginner ,
May 15, 2025 May 15, 2025

Hello,

 

I have a form where I have to allow date formats of m/d/yyyy and just yyyy.  My field format is working great for that.  I found this method online for that:

 

if (!event.value) {
event.value = "mm/dd/yy"; // Default format
} else {
try {
var date = util.scand("mm/dd/yy", event.value);
event.value = util.printd("mm/dd/yy", date);
} catch (e1) {
try {
var date2 = util.scand("yyyy", event.value);
event.value = util.printd("yyyy", date2);
} catch (e2) {
app.alert("Invalid date format. Use mm/dd/yy or yyyy.");
event.value = "";
}
}
}

 

I'm having problems adding validation so that the date cannot be greater than today or older than 1/1/2000,  whether it was entered as m/d/yyyy or yyyy.   Today is 5/14/25, so anything greater than that, older than 1/1/2000  or a year that doesn't fall between 2000 and 2025 should result in a message to user that date is not valid.

 

Thanks to anyone who can steer me in the right direction. 🙂

 

I've been searching out a solution for a few hours.  Does anyone have a suggestion? 

TOPICS
JavaScript , PDF forms
551
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 ,
May 16, 2025 May 16, 2025

@Kristine372929303mo6 maybe, I am doing something easier than that. I created a simple form. And made a PDF from it. I went into Acrobat, and click on 'Prepare Form' and hit 'Start'

creativeexplorer_0-1747384396692.png

I will double click on the first cell, so that it will open the 'Text Field Properties' - Go to 'Format' - Select 'Format' Category - Date - Select the date mm/dd/yyyy and then click on Close to apply

creativeexplorer_1-1747384512479.png
I will do it again in the cell right beside it. 


Click on Close, so that  can test this out...

creativeexplorer_2-1747384642912.png

creativeexplorer_3-1747384685012.png

Try that out?

 



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 ,
May 16, 2025 May 16, 2025

Thank you so much for your reply.  The problem is I need to allow data entry in two date formats in one field.  The user can enter 05/16/25 to indicate course completion date (my project is a grade sheet) or if the course was taken a few years ago and they can't remember the exact date, I have to let them enter the approx. year.  Ex:  2022.   I know, it's a weird thing, but I did figure out that part.  Unfortunately, that eliminates the option of using the calendar drop-down.

 

Kristine372929303mo6_1-1747406947186.png    Kristine372929303mo6_2-1747407007569.png  Kristine372929303mo6_3-1747407123893.png

The field accepts both formats and errors out anything that is not a valid date.  

 

I'd like to go further by validating what was entered to help prevent typos.  Right now, it will accept any date or any 4-digit year.  So, the users could enter a date in the future like 05/16/2027 or 2039.  I know this will happen - sigh.  Anyway, I thought it might help to inject some logic that will kick out an error message if that happens.  I know that won't catch everything, but it will prevent some.  In addition to preventing a future date, I'd like to make sure they can't make the error of entering anything too far in the past.  

 

I'm going to continue to play with this today, but appreciate any further help I can get.

 

Again, Creative explorer, Thank you for taking the time to reply to me.  Have a great day!

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 ,
May 16, 2025 May 16, 2025

You need to use a custom Validation script for this. Basically, it should be similar to your Format script where you figure out what pattern is used and convert it to a Date object. Then you need to convert that object to your two thresholds (today's date and 01/01/2000). If it's not between them, reject the value by setting event.rc to false.

To compare Date objects you can use the getTime method, which converts them to a number. You then compare those numbers to know if one date falls before or after the other.

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 ,
May 18, 2025 May 18, 2025
LATEST

Hi @Kristine372929303mo6 ,

 

In the following example, I utilize a validation script (as suggested by @try67 ) but emphasizing on using a regular expression for the input date rather than custom date formats. 

 

This script compares the year of the input date with both the current year and the year 2000. Additionally, I have restricted the input in the event target field to a maximum of 10 digits.

 

See example script below:

 

var f = event.value;
var inputDate = new RegExp("(^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/([0-9]{4})$|^[0-9]{4}$)");
var enteredDate = (new Date(f)).getFullYear();
var currentDate = (new Date()).getFullYear();
var initialDate = (new Date(2000, 01, 01)).getFullYear();

if (event.value !="") {

 if (event.value && inputDate.test(event.value) == true && (enteredDate >= initialDate && enteredDate <= currentDate)) {

   event.value = f 

   } else {

    app.alert("Please check for errors: \n\n*Enter Date Format as [ Month: \"03\", Day: \"14\", Year: \"2023\" ] or [ Year: 2019 ]\n\n\*Date cannot be older than year 2000\n\n\*Date cannot be greater than today's date");

   event.value = "";
    
  }
}    

 

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