Está en la página 1de 12

Adobe Community: Javascript that affects all text fields

CompTerra

Javascript that affects all text fields


Oct 22, 2009 2:05 PM I am now trying to figure out a script that will affect all text fields in the document, there would be a button that when clicked would affect all text fields in the document. Thank you

2057Views 28 Replies Latest reply: DC Dowd, Apr 26, 2011 2:51 PM Replies

1. Gilad D (try67),
Oct 22, 2009 2:08 PM in reply to CompTerra Report

Can't be done without knowing their names.

| Mark as:

2. CompTerra,
Oct 22, 2009 2:20 PM in reply to Gilad D (try67) Report

I am using the following now: this.getField(["TEXT"]); that affects all fields starting with TEXT...my issue is that not all fileds may start with TEXT.

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields


| Mark as:

3. Gilad D (try67),
Oct 22, 2009 2:24 PM in reply to CompTerra Report

Are you 100% sure about that? It's not documented and I just tried it on a form and it just returns NULL.

| Mark as:

4. CompTerra,
Oct 22, 2009 2:29 PM in reply to Gilad D (try67) Report

100% positive...notice the [ inside the " in this.getField(["TEXT"]) I started with naming the field: this.getField("TEXT.0.1.2") then found I could do the [TEST] inside quotes and that will affect all fields starting with TEXT...text.0 text.0.1 text.0.0.1, and so on.

| Mark as:

5. Gilad D (try67),
Oct 22, 2009 2:32 PM in reply to CompTerra Report

I did, but what you are accessing are actually copies of the same field ("widgets"), not (strictly speaking) separate fields. If you will remove the periods from their names it will not work anymore.

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields


| Mark as:

6. CompTerra,
Oct 22, 2009 2:36 PM in reply to Gilad D (try67) Report

That makes sense... I do have many text fields and each one starts with text.0 and so on, so I am lucky for that, though the issue continues with those fields that may not have that name.

| Mark as:

7. Gilad D (try67),
Oct 22, 2009 2:39 PM in reply to CompTerra Report

Like I said, those fields are copies of one another. If you change the value of one of them, the rest will change as well.

| Mark as:

8. CompTerra,
Oct 22, 2009 2:46 PM in reply to Gilad D (try67) Report

When you say value what do you mean? If I type the text 1234 into TEXT.0 then proceed to another field named TEXT.0.1 and type the text 5432 both values of those fields are different....I am confused in what your saying. Each field is its own field, had I named two fields TEXT.0 then both fields are the same and if I type into one it will show in both.

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields


Mark as:

9. Gilad D (try67),
Oct 22, 2009 2:48 PM in reply to CompTerra Report

But if you create TEXT.0 and TEXT.1 they will have the same value.

| Mark as:

10. CompTerra,
Oct 22, 2009 2:58 PM in reply to Gilad D (try67) Report

Same value as in I copy TEXT.0 and paste somehwere then rename to TEXT.1? That would retain the original value of course until I delete or change it. However if I create TEXT.0 then create TEXT.1 they are both seperate fields and do not seem to retain any of the same values. I just did it to make sure....

| Mark as:

11. Gilad D (try67),


Oct 22, 2009 3:09 PM in reply to CompTerra Report

You're right, it doesn't. It looks like it "tricks" Acrobat to think these are widgets of the same field, when in fact the original field doesn't exist.

| Mark as:

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields

12. CompTerra,
Oct 22, 2009 3:12 PM in reply to Gilad D (try67) Report

This is lucky for me....at least I have the "[TEXT]" option that will address all fields starting with that.. Thank you for your help.....if you can think of anything let me know!!

| Mark as:

13. Gilad D (try67),


Oct 22, 2009 3:21 PM in reply to CompTerra Report

By the way, you don't need the square brackets. getField("TEXT") should work as well.

| Mark as:

14. MarkWalsh,
Oct 22, 2009 3:22 PM in reply to CompTerra Report

Is this what you are looking for? function processTextFields () { for(var i=0;i<this.numFields;i++) { // Loop through all fields on document var fldName = this.getNthFieldName(i); var oField = this.getField(fldName); var a = oField.getArray(); for (j =0; j < a.length; j++) {

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields if (oField.type == 'text') { // Do something here } } } }

| Mark as:

15. CompTerra,
Oct 22, 2009 3:25 PM in reply to MarkWalsh Report

I have not tried yet....this will affect all text fields? I want to be able to click a button and have all text fields affected with the proceeding action.

| Mark as:

16. Gilad D (try67),


Oct 22, 2009 3:35 PM in reply to MarkWalsh Report

Nice one. I totally forgot about this...

| Mark as:

17. MarkWalsh,
Oct 23, 2009 5:37 AM in reply to CompTerra

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields


Report

CompTerra wrote: I have not tried yet....this will affect all text fields? I want to be able to click a button and have all text fields affected with the proceeding action. Yes. The first loop: for(var i=0;i<this.numFields;i++) { will loop thorugh all of the fields in the document. The second loop: for (j =0; j < a.length; j++) { will loop through all of the widgets for that field. You can eliminate the second loop if you don't need to work with the actual widgets. You can change the If statement to something else if you want to process other types of fields (i.e. combo boxes)

| Mark as:

18. DC Dowd,
Apr 22, 2011 10:04 AM in reply to MarkWalsh Report

Could this script be modified to validate all fields in a form before submitting the form by e-mail by doing something akin to what follows? I'm trying to validate fields before using the submitForm function without using Adobe's built-in Submit button because I want to be able to pull form fields to populate the subject line of the e-mail and have been unable to do so with the Submit button (I'm guessing it doesn't allow for code in the "?subject="portion). function processTextFields () { for(var i=0;i<this.numFields;i++) { // Loop through all fields on document var fldName = this.getNthFieldName(i); var oField = this.getField(fldName); var a = oField.getArray(); for (j =0; j < a.length; j++) { if (oField.type == "") { noreturn; } else { return; }

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields }; this.mailDoc({ bUI: false, cTo: "address@example.com", cSubject: "Form" + " " + this.getField("DesiredField01").value + " " + this.getField("DesiredField02").value + " " + this.getField("Date").value, });

| Mark as:

19. Gilad D (try67),


Apr 25, 2011 3:16 AM in reply to DC Dowd Report

Of course you can set the subject. You're doing so in your own code by setting the value of cSubject. If you want to condition sending the email on on the text fields being filled-in, use the loop described earlier in this form to check the value of the fields. If any of them is empty, exit the function by calling "return;", otherwise the script will continue to the line that sends the email. Be aware that there's no such command as "noreturn" in JS.

| Mark as:

20. DC Dowd,
Apr 25, 2011 8:40 AM in reply to Gilad D (try67) Report

Right, then. Here's the code I'm inserting into my document. function processTextFields () { for(var i=0;i<this.numFields;i++) { // Loop through all fields on document var fldName = this.getNthFieldName(i); var oField = this.getField(fldName); var a = oField.getArray(); for (j =0; j < a.length; j++) { if (oField.type == "") {

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields return; } else { this.mailDoc({ bUI: false, cTo: "ddowd@capstoneturbine.com", cSubject: "Form" + " " + this.getField("SiteName01").value + " " + this.getField("SysSN").value + " " + this.getField("Date").value, });} };}} I'm getting nothing when the required fields are not filled in, which I rather expected. However, when all fields are filled in I am still getting no response from the button. The mailDoc bit of code is working fine with no other code attached, but every attempt at validating the form's required fields is causing trouble. A thought occurs to me (I'm new at JS coding), but this code is referencing EVERY field in the document, right? This would mean I should get no result even if the non-required fields are filled out (if my understanding is correct). Can this code be modified to only apply to required fields? Thanks for the help in getting this far. Using Acrobat 9.

| Mark as:

21. Gilad D (try67),


Apr 25, 2011 9:50 AM in reply to DC Dowd Report

You have quite a few mistakes in your code. Use this instead: function processTextFields () { for (var i=0; i<this.numFields; i++) { // Loop through all fields on document var fldName = this.getNthFieldName(i); var oField = this.getField(fldName); // check if the field is a text field, and if it's empty if (oField.type == "text" && oField.value=="") { return; } } // this part is only executed if all the text fields are not empty this.mailDoc({ bUI: false, cTo: "ddowd@capstoneturbine.com", cSubject: "Form" + " " + this.getField("SiteName01").value + " " + this.getField("SysSN").value + " " + this.getField("Date").value }); }

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields


Mark as:

22. DC Dowd,
Apr 25, 2011 10:19 AM in reply to Gilad D (try67) Report

Copied and pasted that code to avoid further mistakes. Still not getting anything, even when all text fields are filled in. Adobe gives no error in the code, just doesn't run it when I extend features and open the form in Reader (which is where those who will use the form will be opening it). I keep shuddering at the thought that someone is going to say I should use the built-in "Submit button" option, which would validate all required fields without the hassle. However, that feature is exactly where I am having trouble getting the subject line of my e-mail to pull the contents of fields in the form. Thanks for the code help. Going to keep chipping away at it and running through the API for more insight.

| Mark as:

23. Gilad D (try67),


Apr 25, 2011 10:23 AM in reply to DC Dowd Report

Are you calling the function from anywhere?

| Mark as:

24. DC Dowd,
Apr 25, 2011 10:28 AM in reply to Gilad D (try67) Report

The script is being run on the mouse up of a button that is being used to submit the form. Not being called from anywhere, just being run when that button is clicked.

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields

| Mark as:

25. Gilad D (try67),


Apr 25, 2011 10:33 AM in reply to DC Dowd Report

Yes, but you must call the function, or the code within it won't execute. Add this as the last line, after everything else: processTextFields();

| Mark as:

26. DC Dowd,
Apr 25, 2011 10:50 AM in reply to Gilad D (try67) Report

Excellent. That works perfectly. Now to look into validating only required fields.

| Mark as:

27. MarkWalsh,
Apr 26, 2011 12:45 PM in reply to DC Dowd Report

Change if (oField.type == "text" && oField.value=="") { to if (oField.type == "text" && oField.value=="" && oField.required) {

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

Adobe Community: Javascript that affects all text fields (Note - since this is only checking Text fields, you will need to make changes if you want it to validate required fields that are not textfields - e.g. radio buttons, checkboxes, etc.)

| Mark as:

28. DC Dowd,
Apr 26, 2011 2:51 PM in reply to MarkWalsh Report

Excellent! Perfect. Works exactly as I need it to work. This forum rocks. Thank you for your help.

| Mark as:

Adobe Community powered by Jive SBS 4.5.6.3 forum software

Jive Software

http://forums.adobe.com/thread/510983?decorator=print&displayFullThread=true[6/27/2013 1:38:42 AM]

También podría gustarte