Skip to main content
Participant
October 7, 2021
Answered

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

  • October 7, 2021
  • 1 reply
  • 1787 views

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!!

 

 

 

Correct answer Nesa Nurani

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);

1 reply

Nesa Nurani
Community Expert
Community Expert
October 7, 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.

 

Participant
October 7, 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);

 

 

Foo0036
Participating Frequently
December 3, 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?


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