Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

How to set an expression per value range & execute based on value entered

Avatar

Level 2

I am looking for help with an IF statement that looks at value ranges and performs a sum based on what it finds.
I am using LC D 8, script is running on FormCalc - Client side - Calculate event. Numericfield1 is a user entered value.

I currently have (which doesn't work):

if ( numericfield1 >= 1000 <= 4999 ) then // I want to check is the users input is between these numbers

numericfield1 * 5 / 1000 // If it is I want to execute this sum only

elseif ( numericfield1 >= 5000 <= 9999 ) then

numericfield1 * 10 / 1000

elseif ( numericfield1 >= 10000 <= 19999 ) then

numericfield * 20 / 1000

endif

So if I enter 2500 in numericfield1 the sum would be: 2500 * 5 / 1000 = 12.5

I want to continue these value ranges in these sets: 1000 to 4999 | 5000 to 9999 | 10000 to 19999 | 20000 to 49999 | 50000 to 250000.

(would also like to enforce that 1000 is the lowest possible entry)

The above will work when numericfield1 is anything below 4999, but anything after that it continues to * by 5 then / by 1000. Where it should * 10 then / by 1000.

Hope someone can push my thinking in the right direction, many thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 10

You need an "and" in your statement:

if (numericfield1 >= 1000 and numericfield1 <= 4999) then

View solution in original post

6 Replies

Avatar

Correct answer by
Level 10

You need an "and" in your statement:

if (numericfield1 >= 1000 and numericfield1 <= 4999) then

Avatar

Level 2

Thank you Jono

I was also missing the second mention of the numericfield1 after the 'and'. Starting to make more sense now.

My gratitude reaches out to you

Avatar

Level 2

Thanks for the reference docs. I shall download them now, from glance I think I have 2 of them already... lengthy reading

A follow on question to the above; If I have a radio button with 2 options and I wanted to execute one of two IF statements based on the radiobutton selection how might I do this? eg:

// Run this if radio button selection = A

if (numericfield1 >= 1000 and numericfield1 <= 4999) then

numericfield * 5 / 1000

endif

// Run this if radio button selection = B

if (numericfield1 >= 1000 and numericfield1 <= 4999) then

numericfield * 20 / 1000

endif

I wondered if I could add something like:

if (radiobutton = A) then

// insert above if statement for 'A' here

elseif (radiobutton = B) then

// insert above if statement for 'B' here

Alternatively I read you can use another numeric field and set a click event on the radiobutton choices like:

numericfield2 = 1 // for radiobutton choice A

numericfield2 = 2 // for radiobutton choice B

From here I could use this numeric fields value in my IF statement sets to perhaps do what I want to do.

Is there a way to nest an IF statement inside another?

** Just thought I'd ask while I trawl the forum is a similar answer

Avatar

Level 5

Please try this - it works fine and I hope you understand it:

script1.jpg

You have to take a look for the binding of your radiobuttonlist here - You can change the binding value with a doubleclick, for example

1 to ValueA

2 to Value B

radioblist.jpg

Then you have to change your script and ask for:

if(RadioButtonList.rawValue == ValueA)

{

    if (NumericField1.rawValue >= 1000 && NumericField1.rawValue <= 4999)

    {

        NumericField1.rawValue *5/1000;

    }       

}else

{

    if (NumericField1.rawValue >= 1000 && NumericField1.rawValue <= 4999)

    {

        NumericField1.rawValue *20/1000;

    }   

}

I would prefer to show the user a message:

if(this.rawValue >=1000 && this.rawValue <=4999)

{

}else

{

    app.alert("Please insert a value between 1000-4999. Thx.");

    this.rawValue = null;

}

You have to coy this in the exit-event of the NumericField1.

Hope it will helps you,

Kind regards Mandy

Avatar

Level 2

Thank you Mandy

That helped very much, after I took the time to understand the code it made sense. I was able to code up the rest of my requirements and evertything worked beautifully. Thank you very much.