Jump to content

Javascript converting/format input of numbers


marcusm

Recommended Posts

Hi!

 

I want a javascript rule that does the following:

 

When you put for an example these numbers in following textfields:

 

Textfield KR: 45

Textfield ORE:90

 

in a textfield KR I want it to print :- after if there is no input in textfield ORE.

 

If there is input in textfield ORE I want textfield KR to print 45: then after comes the textfield variable ORE like this:

 

KR(variable):ORE (variable)

 

Is this possible?

 

And even better:

 

If you have one textfield and someone puts in 45,55 or 45;55, can you get the numbers before "," to act as variable and print 45: and the second to be a variable and print out 55?

 

I want to variables that I can place out in my template.

Link to comment
Share on other sites

I'm a little confused by your post regarding how your data is setup and what you want your output to look like. As best I understand it you are trying to do the following with a couple of assumptions on my part.

 

Assumptions:

1) 1 field ("KR:ORE") with both numbers separated by a semicolon (i.e. 45;55 or 45;90 or 45;0)

2) Output reads "KR#:ORE#"

 

Code:

var values = Field("KR:ORE").split(";");
var myString = "KR" + values[0];
if (values[1] != "0") myString += ":ORE" + values[1];
else myString += ":-";
return myString;

 

Per the sample field values in assumption 1 above, the code should return:

KR45:ORE55

KR45:ORE90

KR45:-

Link to comment
Share on other sites

I'm a little confused by your post regarding how your data is setup and what you want your output to look like. As best I understand it you are trying to do the following with a couple of assumptions on my part.

 

Assumptions:

1) 1 field ("KR:ORE") with both numbers separated by a semicolon (i.e. 45;55 or 45;90 or 45;0)

2) Output reads "KR#:ORE#"

 

Code:

var values = Field("KR:ORE").split(";");
var myString = "KR" + values[0];
if (values[1] != "0") myString += ":ORE" + values[1];
else myString += ":-";
return myString;

 

Per the sample field values in assumption 1 above, the code should return:

KR45:ORE55

KR45:ORE90

KR45:-

 

Hi!

 

If the input is:

 

textfield KR: 45

textfield ÖRE: 44

 

I like the output to be:

 

45:44

 

If the input is:

 

textfield KR: 45

textfield ÖRE: empty (not any input)

 

I like the output to be:

 

45:-

 

so the :- and the : should be in the rule and show up depending on what the input is in the textfields.

Link to comment
Share on other sites

My previous solution was based on this scenario:

And even better:

 

If you have one textfield and someone puts in 45,55 or 45;55, can you get the numbers before "," to act as variable and print 45: and the second to be a variable and print out 55?.

But based on your follow-up, the rule could easily be modified:

var valueKR = Field("KR");
var valueORE = Field("ORE");
var myString = (valueORE != "") ? valueKR + ":" + valueORE : valueKR + ":-";
return myString;

Link to comment
Share on other sites

Hi!

 

I tried the rule and it works as I wanted. Thank you for the help!

 

Now for a follow up question.

 

Is it possible to combine many fields to format in this rule? For the moment there are two fields that are being formatted. We have more combined fields with KR and ORE that have the same formatting.

 

I copied the rule and used it again on two new fields for KR and ORE which worked but if you could combine one rule to put in the variable editor it would really be a good rule to easy in the templates we are designing.

 

What I mean is that I want more than the two fields in the rule to be inserted in the rule:

 

for an example:

 

KR1

ORE1

 

KR2

ORE2

 

Im not skilled in javascript so I'm not sure how to add more fields in the rule that you have created.

Link to comment
Share on other sites

Per your example (KR1, KR2...) I think what you want could be accomplished with a FOR loop, but I need more information. Can you post a sample data file with 2 or 3 records and your field names as well as show how you would like the data to appear on the output?
Link to comment
Share on other sites

Hi again!

 

In one textfield named "Pris" I want the user to be able to input following values (examples below):

 

91

91:90

91;90

91,90

 

 

Then I want the rule to change formating of the input to the following:

 

The output should always be like this:

 

if input is 91 then output should be 91:- (notice the :- at the end)

if input is 91:90 then nothing should format

if input is 91,90 then then output should be 91:90

 

 

I also have a question about the first rule you created that worked great. I have fields in the template that have a prices with taxes and without taxes. Is it possible to automatically fill out fields in the template with a value from one field and then add (+) 25% of the numbers value in another field? So if you put in 100 the field without taxes it fills automatic in 125 in the field with taxes?

 

 

Hope you understand what I mean.

Link to comment
Share on other sites

In one textfield named "Pris" I want the user to be able to input following values (examples below):

 

91

91:90

91;90

91,90

 

 

Then I want the rule to change formating of the input to the following:

 

The output should always be like this:

 

if input is 91 then output should be 91:- (notice the :- at the end)

if input is 91:90 then nothing should format

if input is 91,90 then then output should be 91:90

var pris = Field("Pris");
var result = "";
// first handle single number entries
if (pris.indexOf(/\W/) == -1) {
  result = pris + ":-";
} 
// next handle multi-numbers via an array
else var prisArray = pris.split(/\W/g);
if (prisArray) {
  for (var i=0; i<prisArray.length; i++) {
     result += prisArray[i] + ":";
  }
  result = Left(result, result.length-1);
}
return result;

 

I also have a question about the first rule you created that worked great. I have fields in the template that have a prices with taxes and without taxes. Is it possible to automatically fill out fields in the template with a value from one field and then add (+) 25% of the numbers value in another field? So if you put in 100 the field without taxes it fills automatic in 125 in the field with taxes?

// assume Field("BeforeTax") contains pre-tax amount
var afterTax = Field("BeforeTax")*1.25;

Link to comment
Share on other sites

I tried running the JS-code you wrote but there seems to be a problem because when input is:

 

11;10

 

the rule didn't format the input to 11:10

 

and when input 11:20

 

output: 11:20:- (the :- shouldn't show up)

 

It may have been my bad experience and explaining in javascript and setting of formatting rules :)

 

 

Is it possibly to get two decimals in return on the field afterTax? Watch the example below:

 

beforeTax = 3990

afterTax = 4987.5 (want the output to be 4987.50)

Link to comment
Share on other sites

Is it possibly to get two decimals in return on the field afterTax? Watch the example below:

 

beforeTax = 3990

afterTax = 4987.5 (want the output to be 4987.50)

Yes. You can use the built-in formatNumber() function in FP. You can search the forums for several examples of how to implement it in your code. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...