• 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 and Checkboxes

New Here ,
Aug 26, 2020 Aug 26, 2020

Copy link to clipboard

Copied

I have used the two below scripts with success to cause fields to display "N/A" and become readonly when a checkbox is checked and to revert back to allowing numeric entry only that is centered when unchecking the box. I need to do the same thing except when the box is unchecked, allow only a date to be entered in the field in dd-mmm-yyyy format.

 

I have a checkbox with the following script:

 

var myText = ""; // This holds the text I want entered in all the boxes
var myField; // Field object variable
var myFieldReadonly = false; // boolean for readonly property

/* if the box is checked, then enter N/A into all the fields
and also set to readonly. Otherwise, empty any text and
allow text entry into all fields.
*/
if (event.target.isBoxChecked(0)){
myText = "N/A";
myFieldReadonly = true;
} else {
myText = '';
myFieldReadonly = false;
}

// Loop through items 1 through 2 and set properties
for (x = 1; x <= 2; x++) {
myField = this.getField("Group5.Text" + x);

myField.value = '';
myField.readonly = myFieldReadonly;
myField.value = myText;

if (myFieldReadonly == true) {
myField.alignment = "center";
} else {
switch (x) {
case 1: myField.alignment = "center"; break;
case 2: myField.alignment = "center"; break;

}
}
}

 

I have the below script on a field (under the format tab -> custom -> Custom Keystroke Script) where when the checkbox is unchecked, it makes the field only allow numbers and not letters:

 

if (this.getField("Check Box4").value != null) {
if(!event.willCommit){
event.rc = !isNaN(event.change) || event.change == ".";
//event.rc = /^[\d.,]+$/.test(event.change);
}
}

 

The two fields that this appiles to are text fields with date formating. When I uncheck the box, I want the fields to go back to only allowing a date to be entered in the field.

TOPICS
Acrobat SDK and JavaScript

Views

625

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 ,
Aug 26, 2020 Aug 26, 2020

Copy link to clipboard

Copied

I don't understand why you need to do anything... Just set the fields as having a Date format. When they are editable this will apply. When they are not, it won't matter.

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 ,
Aug 26, 2020 Aug 26, 2020

Copy link to clipboard

Copied

I tried this and it didn't worked out right. 

 

He wants to be able to type only numeric values in the same text field that will be used as a date field when the checkbox is checked.

 

Having this kestroke script doesn't by itself didn't allowed me to format the field using the  util.printd(), for example. 

 

And if the date field is used as an actual date field (with date picker), then it won't accept anything that is not entered as the designated date format. It will throw error.

 

I am unsure if this is even possible, but the only thing that made sense to me was to incorporate  a validation script, which  was posted by Ricardo Falegnami in this old thread: https://answers.acrobatusers.com/Allow-user-enter-numbers-date-populate-slashes-validate-q189846.asp...

 

Adding that script as a custom validation script (like shown below) worked pretty good on my end:

 

 

// adding slashes
var newDate = event.value;
// validate date
if (newDate != "") { // process non-empty string
    var oMyDate = [];
    oMyDate[0] = util.scand("ddmmyyyy", newDate);
    oMyDate[1] = util.scand("ddmmyy", newDate);
    oMyDate[2] = util.scand("ddmyyyy", newDate);
    oMyDate[3] = util.scand("dmmyyyy", newDate);
    oMyDate[4] = util.scand("dmyyyy", newDate);
    oMyDate[5] = util.scand("dmyy", newDate);
    var isDate = false;

    for (var i=0; i<oMyDate.length; i++) {
        if (oMyDate[i] !== null) {
            event.value = util.printd("dd-mmm-yyyy", oMyDate[i]); // strict format
            isDate = true;
            break;
        }
    }

    if (isDate === false) {
        app.alert("Invalid date entered! Expected dd-mmm-yyyy (e.g. ...)", 0, 1, "Date Validation"); // check validity
        event.value = "";
    }
 

}

 

 

It will not allow the user to enter anything that doesn't follow the intended date format and it will  also throw an alert.

 

And then just leave the custom keystroke script as is:

 

 

if(!event.willCommit){
if (this.getField("Check Box4").isBoxChecked(0) == false) {
event.rc = !isNaN(event.change) || event.change == ".";
//event.rc = /^[\d.,]+$/.test(event.change);
  
 } 
}

 

 

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 ,
Aug 27, 2020 Aug 27, 2020

Copy link to clipboard

Copied

LATEST

Thank you for your help so far. And you might be right that what I need isn't possible.

 

But I want to clarify what I need as I tried your above scripts and they did not work for me.

 

I have a checkbox and two text fields set to date format. I need to make it so that if the user checks the checkbox, the text fields display "N/A" and become readonly. If the user then decides to uncheck that box, the fields revert back to only allowing a date to be entered.

 

The JS I have on the checkbox controls the "N/A" and readonly condition and if the box is then unchecked, it resets the formating back to original before the box is ever checked.

 

I don't think the custom keystroke script is correct or maybe not needed as the "NaN" in it stands for "Not a Number" and I need to be able to enter numbers and letters to enter a date. Without the keystroke script entered and still using the validation script, I get the following error: "Invalid date/time: please ensure that the date/time exists. Field [Group5.Text1] should match format dd-mmm-yyyy." So somehow the checkbox script is not overiding the date formating on the text field.

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