Highlighted

Adding a string to beginning of field if field contains user input

New Here ,
Aug 27, 2020

Copy link to clipboard

Copied

Hi all,

I am new to Javascript, but have coded in VBA for a while...very different!

I am trying to figure out how to add a fixed string to the beginning of a field after the user inputs their data.

 

Any help would be awesome!

 

Thanks...

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

As the field's custom Format script enter this code:

if (event.value) event.value = "AWARDS: "+event.value;

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

83

Likes

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

Adding a string to beginning of field if field contains user input

New Here ,
Aug 27, 2020

Copy link to clipboard

Copied

Hi all,

I am new to Javascript, but have coded in VBA for a while...very different!

I am trying to figure out how to add a fixed string to the beginning of a field after the user inputs their data.

 

Any help would be awesome!

 

Thanks...

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

As the field's custom Format script enter this code:

if (event.value) event.value = "AWARDS: "+event.value;

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

84

Likes

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
Aug 27, 2020 0
Most Valuable Participant ,
Aug 27, 2020

Copy link to clipboard

Copied

Do you want the string to be an actual part of the field's value, or just to be displayed? If the former, keep in mind that each time the user edits the field it will add the string again to the start of it, unless your script checks for that first.

Likes

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
Reply
Loading...
Aug 27, 2020 0
New Here ,
Aug 27, 2020

Copy link to clipboard

Copied

The idea is that if a user inputs data into a field, it will add the string "AWARDS:" to the beginning of what they type. If the field is null, then it wont.

I was thinking of using BLUR for the validation since I don't want the action to take place until after the user is done.

 

I actually thought this would be simple, but in JAva, not so much! Still trying to figure this language out...

Likes

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
Reply
Loading...
Aug 27, 2020 0
New Here ,
Aug 27, 2020

Copy link to clipboard

Copied

Just realized whata you were asking...

I guess it doesn't really matter which way. Part of the field input or not, but I can see the advantages to it "Apprearing" at outside of the field if the field is not null.

If inside of the field, I was thinking something like a Concatenate. Just add "AWARDS:" & User string

IF outside of field (at lead) then only if field is not null or >""

Likes

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
Reply
Loading...
Aug 27, 2020 0
Most Valuable Participant ,
Aug 27, 2020

Copy link to clipboard

Copied

As the field's custom Format script enter this code:

if (event.value) event.value = "AWARDS: "+event.value;

Likes

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
Reply
Loading...
Aug 27, 2020 1
New Here ,
Aug 27, 2020

Copy link to clipboard

Copied

Perfect! I am starting to get some basic understanding...

 

Thanks a bunch for the help!

Likes

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
Reply
Loading...
Aug 27, 2020 0
New Here ,
Aug 28, 2020

Copy link to clipboard

Copied

try67,

OK...I have beenb playing with this if statement, and am not able to get it to work...

 

If you could help me undersstand this better, I would appreciate it.

 

var naward = this.getfield("Awards").value

if(naward==null) {

event.value="AWARDS: "+event.value

}

else if (naward!=null) {

event.value=naward+event.value

}

 

I am putting this in the "Run custom validation script" field, but when I enter anything into that field, it will only return what I enter.

As I stated in my original post, I am a beginner with Java and Adobe when it comes to writing scripts, so I am sure I am messing something up. All of the research I have found on the internet and this site is all about adding numbers from various fields, and not what I am trying to accomplish.

In short...help? Also, if you could explain how it works, that would be great too.

 

Likes

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
Reply
Loading...
Aug 28, 2020 0
Most Valuable Participant ,
Aug 28, 2020

Copy link to clipboard

Copied

Why are you not placing it where I said it should go?

Likes

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
Reply
Loading...
Aug 28, 2020 0
Most Valuable Participant ,
Aug 28, 2020

Copy link to clipboard

Copied

Also, why did you change the code I provided?

Likes

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
Reply
Loading...
Aug 28, 2020 0
New Here ,
Aug 28, 2020

Copy link to clipboard

Copied

try67,

I am still using your iriginal script, but was trying to take it to another level.

 

What if I wanted to add additional "Awards" to the field. I did not want it to return AWARDS again at the beginning of the new entry as well as at the beginning of the field. i.e. Awards: GWAT, ARCOM AWARDS: CAB, etc...

Just trying to figure this stuff out, and doing is learning.

 

 

Likes

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
Reply
Loading...
Aug 28, 2020 0
New Here ,
Aug 28, 2020

Copy link to clipboard

Copied

try67,

OK...I see where I went wrong. I put your original script in the wrong place. Have put it in the right place and it does what I want, but...

 

I am still trying to learn how to use an IF then statements...

 

You can disregard. I will continue trying to learn and come back when I really need the help!

Likes

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
Reply
Loading...
Aug 28, 2020 0
Most Valuable Participant ,
Aug 29, 2020

Copy link to clipboard

Copied

You may also want to take precautions to avoid ending up with a field containing AWARDS: AWARDS: AWARDS: ... if the user tries to correct their input. In general, changing what the user types is a bad idea because it conflicts with the user's normal editing.

Likes

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
Reply
Loading...
Aug 29, 2020 0
Most Valuable Participant ,
Aug 29, 2020

Copy link to clipboard

Copied

That won't happen if they use the script I provided under the Format event.

Likes

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
Reply
Loading...
Aug 29, 2020 0
Most Valuable Participant ,
Aug 29, 2020

Copy link to clipboard

Copied

Indeed. Format good. Validate bad. onBlur bad. People do have a habit of using random events rather than finding out the right one... The "user is done" idea is also a dangerouus one. It's entirely normal to return several times to the same field. This is as bad as trying to force people to fill forms "in order".

Likes

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
Reply
Loading...
Aug 29, 2020 0
try67 LATEST
Most Valuable Participant ,
Aug 29, 2020

Copy link to clipboard

Copied

Agreed. Another pet peeve of mine is resetting forms after submitting them, as if the user will not want to save a copy of the file they sent on their machine... People learn the hard way that this is bad practice once users start contacting them angry about why all the data they sent is "lost".

Likes

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
Reply
Loading...
Aug 29, 2020 0