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

How can I have 2 different decimal number formats in the same field.

Community Beginner ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

Time to ask the experts for help again.

I have 2 text fields. The first is an initial value and the second field is the value after the device is adjusted. If the first field is within a range, the second field populates with 'N/A'. Both fields will always be a decimal number starting with zero (i.e. 0.049 or 0.0493). This number must have 3 decimal places or 4 decimal places and must have the leading zero. The second field must have the same number of decimal places as the first.

I know I can use the Arbitrary Mask (0.9999) or (0.999) to achieve 1 of the results but is it possible (with code) to achieve both options.
The requirements are;
The number must start with 0. (zero and a decimal point).
The number can only have either 3 or 4 decimal places.
The second field must have the same decimal places as the first field. (I think I have the correct code in the second field already for this)


I initially had no formatting in the first field, apart from the limit to the number of characters. The script initially came about because when someone was testing it, they were not inputting the leading zero (i.e .049). So I was tasked to fix it. Personally, I think that users should just be told to enter it the correct way but here we are.

In the first field, I currently have a limit of 6 characters set in the option field and a custom keystroke script (see below). That sort of works but allows the user to enter any value up to 6 digits including the decimal point.

 

if (event.change) event.rc = /^[0-9,\.]+$/.test(event.change);

 

If possible, how would I modify it to suit the requirements above. We could probably live with the leading zero being any digit if that is easier.

----------------------------------------------------------
In the second field, I have the code below, which does work except as soon as I tab into it, it shows 0.000 or 0.0000 (dependant on the first field). I would rather it remains blank until the value is being entered. I did try this as validate code but it did not work.

How can I avoid the zeros appearing. If there is code to solve for formatting field 1, I would also use it in this field


var ff = this.getField("FirstField").valueAsString;
if (Number(ff)>=0.0380 && Number(ff)<=0.0420){
event.value = "N/A";
event.target.textColor = color.black;
}
else if (ff!="" && event.value.length!=0 && (event.value<0.0380 || event.value>=0.0420)) {
event.target.textColor = color.red;
app.alert("WARNING: The value is outside the range of 0.0380 to 0.0420")
}
else if (ff.length==6){
event.value = util.printf("%1.4f",event.value);
event.target.textColor = color.black;
}
else if (ff.length==5){
event.value = util.printf("%1.3f",event.value);
event.target.textColor = color.black;
}
else if (ff==""){
event.value = "";
event.target.textColor = color.black;
}

 

Thanks for any assistance you can provide.

TOPICS
JavaScript

Views

198

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
community guidelines
Community Expert ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

Under which event did you place the last code?

Votes

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
community guidelines
Community Beginner ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

I placed it in the second field as a custom calculation script.

 

Votes

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
community guidelines
Community Expert ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

And what did you select under Format?

Votes

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
community guidelines
Community Beginner ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

At this stage, I have no format set in the second field as I was hoping if there was a solution to my first question about the first field, that I could reuse that.

 

I wont be able to reply to any more posts until tomorrow morning unfortunately.

Thanks for having a look at it for me.

Votes

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
community guidelines
Community Expert ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

OK. But should the second field be editable by the user, or does it have a calculated value?

Votes

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
community guidelines
Community Beginner ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

The user has to manually enter the value in the second field. The only time they wouldn't, is when the value in the first field is within a certain range and than the second field should automatically populate with "N/A".

Votes

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
community guidelines
Community Expert ,
Dec 12, 2024 Dec 12, 2024

Copy link to clipboard

Copied

That is more complicated to implement. You will need a custom calculation script for when the field's needs to show "N/A", a custom validation script for when the value is outside the allowed range, and a custom Format script to format the value entered differently based on the other field.

Votes

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
community guidelines
Community Beginner ,
Dec 13, 2024 Dec 13, 2024

Copy link to clipboard

Copied

LATEST

Thanks TRY67, You pointed me in the right direction. I never thought to use a custom Format script as I usually use just the standard formats available.

I think I have got it as good as I am going to.

I've attached a pdf with just the 2 fields in it. If you can find a way to improve it, I would appreciate it.

A warning, the app alerts are annoying when you are testing different scenarios but in the real world would not be a problem

Votes

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
community guidelines