Click here to Skip to main content
14,084,827 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi all,
I'm working in PDF and have searched high and low for this code but am officially stumped.

I have two text boxes: when I enter a value in the first, I need the second box to reflect that value + 5.

For example:
Box one = 3:00
Box two would automatically become 3:05

What I have tried:

​var firstValue = this.getField("valueOne"). ValueAsString;
var secondValue = this.getField("5");
if (firstValue=="3:00" || secondValue=="3:05") event.value = "5";
else event.value = Number(firstValue) + Number(secondValue);
Posted
Updated 4 days ago

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First of all, Java and Javascript are two different beast, it one of them but no both.
As far as I can see, the question is not related to PDF.

Did you named a field "5" ?
var secondValue = this.getField("5");

It is rather unusual.

Working out something of your code is more a guess than anything else.
​var firstValue = "3:00";
var secondValue = "3:05";
if (firstValue=="3:00" || secondValue=="3:15") event.value = "5";
else event.value = Number(firstValue) + Number(secondValue);

This way, we don't have to guess what is doing the code.

Looks like you are dealing with time values, you can't expect conversion to number to be handled correctly.

And your coding style is bad, it save 2 enter key typing at the expense of readability, this is better.
​var firstValue = this.getField("valueOne"). ValueAsString;
var secondValue = this.getField("5");
if (firstValue=="3:00" || secondValue=="3:05")
    event.value = "5";
else
    event.value = Number(firstValue) + Number(secondValue);


Asking questions is a skill[^]
Some guidelines for posting questions in the forums[^]

[Update]
Quote:
As it is apparently abundantly clear, I am not a professional coder. I have no idea what I'm doing - hence the "bad code" and not knowing the difference between Java and JavaScript.

We can't guess what is not told.
Advice: Learn language and programming properly with some tutorials, not by starting with a personal project.
What you do is like trying to get a driving license by winning a Formula 1 Grand Prix, it don't work like that.
Quote:
I got a Syntax error for an illegal character on line 2.

Did you forgot to tell us that you had the same error message in code from question?
Rule of thumb, to do the same thing on 2 fields, use the same syntax.
Try:
​var firstValue = this.getField("Text23"). ValueAsString;  // use no space before ValueAsString
var secondValue = this.getField("Text24").ValueAsString;  // use same syntax for same operation

Quote:
Would it be easier to have a set value of 5 in another box? So I input a value in Text23 + other box with set value of 5 = result appears in Text24.

Advice: explain the meaning of what you are doing.
I have a form to do this ...
I read value from 2 fields, their meaning is ...
I do this operation which mean ... , the result means ...
I store result in ... field ...
   
v2
Comments
killerkats123 3 days ago
   
As it is apparently abundantly clear, I am not a professional coder. I have no idea what I'm doing - hence the "bad code" and not knowing the difference between Java and JavaScript. I scoured the internet for this result, to no avail, hence my post on this forum. This code is indeed needed for PDF.

The first box where the value is entered is called "Text23" and the result box, where I would like 5 to be added is called "Text24". Based on your suggestion above, I have edited and tried the below code, which still does not work. I got a Syntax error for an illegal character on line 2.

​var firstValue = this.getField("Text23"). ValueAsString;
var secondValue = this.getField("Text24");
if (firstValue=="3:00" || secondValue=="3:05")
event.value = "5";
else
event.value = Number(firstValue) + Number(secondValue);

Would it be easier to have a set value of 5 in another box? So I input a value in Text23 + other box with set value of 5 = result appears in Text24.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 15 May 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100