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

Date Field Validation

Community Beginner ,
Jul 22, 2024 Jul 22, 2024

Copy link to clipboard

Copied

I have formatted a text field into a Date format in the yyyy-mm-dd format. 

 

This input for this text field is then used for a seperate calculation so its important the user only enters in the above format. 

 

The above format works but there are two corner cases:

 

Corner case 1:

If the user enters 2024-05-5 it will autocorrect to 2024-05-05. While the displayed value is correct the user input is wrong and it affects other calculations. To fix this i added validation code to force user to enter at least 10 charachters. 

 

var minAllowedChars = 10;
if (event.value.length>0 && event.value.length<minAllowedChars) {
	app.alert("Invalid date format");
	event.rc = false;
}

 

Corner case 2:

The second issue is if the user enters blanks. For ex. if the user enters "2024-03-4 " it will autocorrect to 2024-03-04. While the displayed value is correct the user input is wrong and it affects other calculations. I tired to add validation code to not allow spaces to be entered but it doesnt seem to work correctly.  For some reason the below code only works on what was already entered and not on what is newly entered.

 

var a = this.getField("Date").value;
var a1 = a.substr(0,1);
var a2 = a.substr(1,1);
var a3 = a.substr(2,1);
var a4 = a.substr(3,1);
var a5 = a.substr(4,1);
var a6 = a.substr(5,1);
var a7 = a.substr(6,1);
var a8 = a.substr(7,1);
var a9 = a.substr(8,1);
var a10 = a.substr(9, 1);


if (event.value.length > 9){
if (a1 == " " || a2 == " " || a3 == " " || a4 == " " || a5 == " " || a6 == " " || a7 == " " || a8 == " " || a9 == " " || a10 == " "){
app.alert("Invalid date format");
event.rc =false;
}
}

 

 

 

 

TOPICS
Acrobat SDK and JavaScript , Windows

Views

30

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 Expert ,
Jul 22, 2024 Jul 22, 2024

Copy link to clipboard

Copied

LATEST

1. So what's the issue there, exactly?
2. That's because you're looking at the value of the field, instead of the event.
But these codes are very cumbersome. Just use this, instead of them both:

 

if (event.value && /^\d{4}-\d{2}-\d{2}$/.test(event.value)==false) {
	app.alert("Invalid date format");
	event.rc = false;
}

Votes

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