Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Value entered does not match the format of the field - HELP

Community Beginner ,
Oct 07, 2021 Oct 07, 2021

I know there are a million questions about this. I swear I've read them all. They are all nuanced and for a completely illiterate JavaScript person, I'm dumfounded.

 

I have a form with several division calculations. The first is calculating correctly, when information is put into the fields, but before I get to the calculation in question, every other field I type into gives me the error.

I need to divide two numbers (both whole numbers) and the answer needs to be a percentage. So my first field "A" has no formatting, the second field "B" also has no formatting. The quotient field is formatted as a percentage. Here is my JavaScript formula:

 

event.value = (this.getField("TenantSqFt").value / this.getField("ProjectSqFt").value);

 

I read somewhere that calculations don't like formatting. That's great, but when I take away the percentage formatting for the quotient field it gives me a very lengthy decimal. Note that I do not get the error at all when I take the percentage formatting off the quotient field.

 

So either I'm doing something wrong, or I need to adjust the Java Script formula to adjust for this. I can change the formula for it to appear like a percent by multiplying by 100, but I still get the lengthy decimal:

 

event.value = ((this.getField("TenantSqFt").value / this.getField("ProjectSqFt").value) * 100);

 

Ideally my A and B fields would be able to be formatted as numbers (so they can have commas for the thousands place) but if that's a deal breaker I'll have to live with it.

 

So then the next question becomes, do I need to throw rounding into the mix? And how on earth do I do that??

 

Thank you fine folks!!

 

 

 

TOPICS
PDF forms
1.9K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Oct 07, 2021 Oct 07, 2021

Use this:

var a = Number(this.getField("TenantSqFt").valueAsString);
var b = Number(this.getField("ProjectSqFt").valueAsString);
var x = (a/b)*100;
if(b == 0) event.value = "";
else
event.value = x.toFixed(2);

View solution in original post

Translate
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 ,
Oct 07, 2021 Oct 07, 2021

Thats because you try to division with zero, try like this:

if(Number(this.getField("ProjectSqFt").valueAsString) == 0)

event.value = "",

else

// your code goes here

You can use "toFixed()" to round to how many decimals you want.

 

Translate
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 Beginner ,
Oct 07, 2021 Oct 07, 2021

Thank you for your reply. I'm a complete no-nothing about JavaScript, so I'm not sure exactly what to type. I tried this, but no result.

 

if(number(this.getField("TenantSqFt/ProjectSqFt").valueAsString) == 0) event.value = ToFixed (((this.getField("TenantSqFt").value / this.getField("ProjectSqFt").value) * 100),2);

 

 

Translate
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 ,
Oct 07, 2021 Oct 07, 2021

Use this:

var a = Number(this.getField("TenantSqFt").valueAsString);
var b = Number(this.getField("ProjectSqFt").valueAsString);
var x = (a/b)*100;
if(b == 0) event.value = "";
else
event.value = x.toFixed(2);

Translate
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 Beginner ,
Oct 07, 2021 Oct 07, 2021

I feel very dense, but I tried this and I'm getting a Syntax Error...

 

if(Number(this.getField("ProjectSqFt").valueAsString); == 0) event.value = ""; else event.value = (Number(this.getField("TenantSqFt").valueAsString); / Number(this.getField("ProjectSqFt").valueAsString);) * 100;.toFixed(2);

Translate
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 ,
Oct 07, 2021 Oct 07, 2021

There are multiple errors in your script.

Is there reason you don't use script I gave you?

Translate
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 ,
Oct 07, 2021 Oct 07, 2021

Semi-colons should only be added at the end of each line, not in the middle of it.

Translate
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 Beginner ,
Oct 07, 2021 Oct 07, 2021

Thank you. I was making it harder than it needed to be. Your script exactly as you wrote it works perfectly.

Translate
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 ,
Dec 03, 2024 Dec 03, 2024

Hey, I don't know if you are still doing this, but I am trying something similar and cannot get the code to work. 

 

What I am doing is 2 variables divided by each other, then divided by 100.  Here is what I have of the script to you gave: 

var a = Number(this.getField("ACTROI1").valueAsString);
var b = Number(this.getField("AmountReq").valueAsString);
var x = (a/b)/100;
if(b == 0) event.value = "";
else event.value = x.toFixed(2);

 

Both variables will be 0 at the start of the form, I normally have the field of the calculation formatted to percentage, I have tried changing it so see if that would work, but no luck.

 

Can you please help?

Translate
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 ,
Dec 03, 2024 Dec 03, 2024

Ok, I don't know how to math, so I need to Multiply by 100, still doesn't work when trying that. 

Translate
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 ,
Dec 03, 2024 Dec 03, 2024

Check the Javascript console (ctrl-j) for errors.

Translate
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 ,
Dec 04, 2024 Dec 04, 2024

Finally figured out how to get the console up, it would not work on my other laptop. The errors are:

TypeError: getField(...) is null
2:AcroForm:Text1:Calculate

Translate
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 ,
Dec 04, 2024 Dec 04, 2024

The error message means that one of the fields doesn't exist.

Translate
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 ,
Dec 04, 2024 Dec 04, 2024

I have check the field name 3 times and it keeps doing that. But that's alright I managed to find some different code that works for my form. Thank you both for your help. 

Translate
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 ,
Dec 03, 2024 Dec 03, 2024

What exactly doesn't work, field doesn't show result or result is incorrect?
Check that field names are correct.

If you have field format as percentage, you don't need toFixed() just set in format to 2 decimals.

Translate
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 ,
Dec 04, 2024 Dec 04, 2024

The field names are correct (I copy and pasted the names), and I have tried with and without the formatting. It doesn't seem to calculate anything when I have the fields filled in. 

Translate
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 ,
Dec 04, 2024 Dec 04, 2024
LATEST

Can you share the file?

Translate
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