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

Multiple acceptable formats for a single date field

New Here ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

Hi, I have a requirement to create a single form field that could accept multiple date formats and not throw an error. Example my date field should be able to accept mmm-dd-yyyy or mmm-yyyy or yyyy , a user should be allowed any of the formats and it should not throw an error. If there any script that I can use ?

TOPICS
JavaScript , PDF forms

Views

243

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

correct answers 3 Correct answers

Community Expert , Nov 13, 2024 Nov 13, 2024

Try this as the field's custom Validation script:

 

 

var acceptedFormats = ["mmm-dd-yyyy", "mmm-yyyy", "yyyy"];
if (event.value) {
	var accepted = false;
	for (var i in acceptedFormats) {
		if (util.scand(acceptedFormats[i], event.value)!=null) {
			accepted = true;
			break;
		}
	}
	if (!accepted) {
		app.alert("The entered value is not in one of the accepted formats and will be rejected.");
		event.rc = false;
	}
}

 

Edited: small mistake in the code fixed

 

Votes

Translate

Translate
Community Expert , Nov 13, 2024 Nov 13, 2024

You can't do that. The calendar widget can only work with a single pattern. So you have to choose one or the other. If you want to use the script I provided you must set the Format to None.

Votes

Translate

Translate
Community Expert , Nov 14, 2024 Nov 14, 2024

You can do it using this code as the field's custom Format script:

 

if (event.value) {
	var numHyphens = event.value.match(/-/g);
	numHyphens = (numHyphens==null) ? 0 : numHyphens.length;
	for (var i=numHyphens; i<2; i++) event.value = "-"+event.value;
}

Votes

Translate

Translate
Community Expert ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

Try this as the field's custom Validation script:

 

 

var acceptedFormats = ["mmm-dd-yyyy", "mmm-yyyy", "yyyy"];
if (event.value) {
	var accepted = false;
	for (var i in acceptedFormats) {
		if (util.scand(acceptedFormats[i], event.value)!=null) {
			accepted = true;
			break;
		}
	}
	if (!accepted) {
		app.alert("The entered value is not in one of the accepted formats and will be rejected.");
		event.rc = false;
	}
}

 

Edited: small mistake in the code fixed

 

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
New Here ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

Thanks Try67,, should I drag the box as a date field and then under "Format" choose custom and then go to the tab "validate" and use this script there ? Since I still need the calendar to show up in the field. Or should I use just a "text" field and then add this under "Run custom validation script"

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
Community Expert ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

I don't think you can have both (calendar and this validation script) because you have to select one date format for the calendar to show up, and the format has a keystroke script that won't accept the other formats.

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
Community Expert ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

You can't do that. The calendar widget can only work with a single pattern. So you have to choose one or the other. If you want to use the script I provided you must set the Format to None.

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
New Here ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

Hi Try67,, can you help me add to the script such that if the user uses "mmm-yyyy" then the value changes to "-mmm-yyyy"  and if the user does "yyyy" then the value changes or defaults to "--yyyy" , so it basically also adds dashes for the two formats as a prefix 

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
Community Expert ,
Nov 14, 2024 Nov 14, 2024

Copy link to clipboard

Copied

Do you want to actually change the value, or just how it is displayed?

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
New Here ,
Nov 14, 2024 Nov 14, 2024

Copy link to clipboard

Copied

How its displayed if some chooses to enter "mmm-yyyy" it should display "-mmm-yyyy" if someone enters "yyyy" it should display "--yyyy"

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
Community Expert ,
Nov 14, 2024 Nov 14, 2024

Copy link to clipboard

Copied

LATEST

You can do it using this code as the field's custom Format script:

 

if (event.value) {
	var numHyphens = event.value.match(/-/g);
	numHyphens = (numHyphens==null) ? 0 : numHyphens.length;
	for (var i=numHyphens; i<2; i++) event.value = "-"+event.value;
}

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