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 ?
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
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.
Copy link to clipboard
Copied
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;
}
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
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"
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.
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.
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
Copy link to clipboard
Copied
Do you want to actually change the value, or just how it is displayed?
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"
Copy link to clipboard
Copied
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;
}