Skip to main content
Participating Frequently
November 13, 2024
Answered

Multiple acceptable formats for a single date field

  • November 13, 2024
  • 1 reply
  • 1128 views

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 ?

This topic has been closed for replies.
Correct answer try67

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


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

1 reply

try67
Community Expert
Community Expert
November 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

 

Participating Frequently
November 13, 2024

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"

PDF Automation Station
Community Expert
Community Expert
November 13, 2024

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.