LiveCycle - Multiple calculations dependent upon check boxes selected and an input field? - javascript

Doing a form in LiveCycle and using FormCalc. As a part of the form I have 2 check boxes (cbPhoto) & (cbCert), and 2 numeric fields (numPages) and (numResults).
What I would like to achieve is when one or both of the check boxes are selected and an number is entered into (numPages) then do the following calculations and display the results in (numResults).
Calculations:
If (cbPhoto) is selected and say for example 5 is entered into (numPages) I want the calculation to work out $20 for the first page and $1 for every page thereafter (so in this case the results would be $24).
If (cbCert) is selected and say for example 5 is entered into (numPages) I want the calculation to work out $22 for the first page and $2 for every page thereafter (so in this case the results would be $30).
If both check boxes are selected and say for example 5 is entered into (numPages) I want both of the above calculations to run (the results being $54).
I want the (numResults) to remain blank until after a number is placed in (numPages) regardless if the check boxes are selected or not.
I have had very limited success with the following code and would be happy to use JavaScript if it will make it work.
A9.#subform[0].A4::calculate - (FormCalc, client)
if (cbP3==1) and (cbC3==1) then
P1-1+20
and
(P1-1)*2+22
else ""
endif
Any and all help would be greatly appreciated. Cheers

Here is my JavaScript solution. The "calculate" event of numResults will execute whenever any of the other 3 fields change. A non-blank result will be displayed when numPages has a value and at least one of the checkboxes are checked.
form1.#subform[0].cbCert::change - (JavaScript, client)
numResults.execCalculate();
form1.#subform[0].cbPhoto::change - (JavaScript, client)
numResults.execCalculate();
form1.#subform[0].numPages::change - (JavaScript, client)
numResults.execCalculate();
form1.#subform[0].numResults::calculate - (JavaScript, client)
var result = null;
if (numPages.rawValue > 0){
result = 0;
if (cbPhoto.rawValue == 1){
result = numPages.rawValue - 1 + 20;
}
if (cbCert.rawValue == 1){
result += (numPages.rawValue - 1) * 2 + 22;
}
if (result == 0) {
result = null; //show blank instead of 0
}
}
//this line sets the value of the results field:
result;

Related

PDF java script resulting in “OFF” value in output field due to off state of check box

For most part this script in this PDF form works fine, it checks if Check Box4 is checked and if it is, it results in a calculation of Level + Check Box4 thru 7 (on state off check boxes have a vaule of 2). The problem occurs that if any of Check Box5, 6 or 7 check boxes are in an off state, the calculatiuon returns the word "OFF" into the field as their output. The calculation still works in the end as this field is then part of a further calculation, but visibly this is bad.
This funciotn is used 35 times on the first page of the pdf alone
Advice on how to retreive the on value but ignore the off value of a check box would be appreciated.
<code>
var v1 = this.getField("Check Box4").value;
if (v1 /= "") {
event.value = this.getField("Level").value
+ this.getField("Check Box4").value
+ this.getField("Check Box5").value
+ this.getField("Check Box6").value
+ this.getField("Check Box7").value; }
if (isNaN(v1) || v1 === "") {event.value = "0";}
</code>
screen shot of resulting yuck
page screen shot
One thing I do pretty often is replace the "Off" with something else, such as a space or even the empty string, as in:
this.getField("myCheckBox").valueAsString.replace(/Off/gim, "") ;
If your return values are non-numeric, you can use value instead of valueAsString.
And that should do it.

Need to extract only integers from string in textarea, then move the result to another textarea

I have textarea:
type="text" value="H0???" which I cant change.
H & 0 are already filled in chars -predefined so users input only 3 random numbers after that. I need to extract those three numbers from that field (excluding "H" and also the leading "0").
so lets say input is like: H0987 (textarea that user fills in).
I need to have a click button which will execute extraction to another text area. Result has to be as in example 987.
Advanced #1 question :) if anyone bothers..:
Better output would be to add comma ":" after the numbers as well so my desired output would be "987:" after the click.
Advanced #2 question :) if anyone bothers..:
when 987: is extracted to new textarea, I need then fill in also another 3 numbers based on 3 dropdowns they chose respectively.
dropdown has 3 options (lets say some colors)
dropdown has 3 options (lets say some other strings)
dropdown has 3 options (lets say some other strings)
so is there a way to add also after "extracted numbers" + ":" + "some1.dropdownoption_option" + "some2.dropdownoption_option" + "some3.dropdownoption_option" ?
Desired output: 987:123 where 1, 2, 3 after : represents the options users picked in dropdown. Not values themself in the option menu but assigned numbers to those options which in result are seen as numbers.
I prefer javascript as rest of my code is running that.
Thank you very much. This community is awesome.
I was able to get it working based on site examples. There was no need for dropdown calls as I stated in the question but I needed checkboxes instead. Also, string was starting with "B" so I had to check actually for last char with (value.length-1). Also I used checkbox instead of button. Code can definitely be improved.
<input id="hostname" name="hostname" type="text" value="BR0???" class="hostname"/>
<input id="comtest" name="comtest" type="text" class="commmunity"/>
<input id="comm_check1" name="comm_check1" type="checkbox" onchange="f_set_comm()"/>
function f_set_comm() {
//extract integers
var value = document.getElementById("hostname").value;
var numbers = value.match(/\d+/g).map(Number);
//add :9
document.getElementById("comtest").value = numbers;
document.getElementById("comtest").value = document.getElementById("comtest").value + ":9";
//check type and add 1 or 2 based on last char A or B in hostname
if (value.substring(value.length-1)== "A")
document.getElementById("comtest").value = document.getElementById("comtest").value + "1";
if (value.substring(value.length-1)== "B")
document.getElementById("comtest").value = document.getElementById("comtest").value + "2";
if (!((value.substring(value.length-1)== "A")) && (!(value.substring(value.length-1)== "B")))
document.getElementById("comtest").value = document.getElementById("comtest").value + "0";
//remove all strings if unchecked
if (!document.getElementById("comm_check1").checked)
document.getElementById('comtest').value = '';
}

How to always show the number prefix in html input field?

Is there any smart, minimalistic way to always show the prefix (positive, negative) of a number in a HMTL input field? (e.g. +1, 0, -1)
I have only found s solution for PHP:
How to prefix a positive number with plus sign in PHP
I have to use <input type="text"> since there are different implementations for text=number in different browsers: Localization of input type number
Why am I doing this?
I have an input field that shows the percentage that can be added (or subtracted) to a certain value.
Basevalue: 10
Mofification %: +10
Results: 11
The easiest way would be using some basic javascript. Add a script at the end of your HTML page (before the body closing tag) then give to the input an id, for example prefixedInput. Then you can write your little script
var inputField = document.getElementById("#prefixedInput");
var inputFieldValue = inputField.value;
if (inputFieldValue > 0) {
inputField.value = "+" + inputFieldValue;
}
if (inputFieldValue < 0) {
inputField.value = "-" + inputFieldValue;
}
Now, that works in a way that isn't really useful because this function will be executed just one time when the page will load, so if you have assigned to your input a value, this will be prefixed with its sign. However if you want to bind this behaviour to some actions (e.g. prefixing the value even if the user inserts the value after the intial page load) you will be forced in using event listeners.

Why does a function that's almost a copy of another fail to update output field?

I'm working on making a PO form that will eventually submit the values to a database. When you look at the code, if you load it, you'll see the line items area. Quantity, Description, Loaction/Use and line total. Everything works great on the first line item, but when filling out the second line item, and the subsequent ones, the line total column doesn't calculate or show any values. The line total field is simple math; quantity * unit price = line total.
Again, the first rown works great. The second row and the others, well, nothing shows up in the line total field to the right and I can't figure out why. The calculations and functions called are all the same, the field IDs are all unique, I'm not getting any errors what so ever. I searched around but didn't find any answers, its an odd one and not sure if I'm searching with the right criteria or not. Here's a link to the page (view source on it to see the code - for now its just simple HTML & Javascript):
http://www.acsout.com/maintenance.html
So when the page loads, 7 of the rows are hidden with this:
document.getElementById('frow3').style.display = 'none';
For each row, to calculate the line total, I'm using this:
function l1calc(){
if (document.getElementById('up1').value != '' && document.getElementById('q1').value != ''){
tot1 = document.getElementById('q1').value * document.getElementById('up1').value;
document.getElementById("lt1").value = parseFloat(Math.round(tot1 * 100) / 100).toFixed(2);
}
}
function l1calc() works fine
The next one, that isn't working, is the same, with different variables:
function l2calc(){
if (document.getElementById('up2').value && document.getElementById('q2').value){
tot2 = parseFloat(document.getElementById('q2').value) * parseFloat(document.getElementById('up2').value);
document.getElementById('lt2').value = tot2;
}
}
The link i posted brings you to the page which will let you view source to see the full code and test it to see what i'm referring to.
If I change (in function l2calc) the last line to be:
document.getElementById('lt1').value = tot2;
then the lt1 field shows the value when the function is triggered. But when it is as it should be for l2calc
document.getElementById('lt2').value = tot2;
the input field lt2 never shows the value, it just stays blank. The same goes for the rest of the fields in the line total column.
This line is wrong in your script (in posubtotal)
if(document.getElementById("lt2").value = null){ // <-- == null
polt2 = 0;
}
I used firebug, and saw the hardcoded 568 in l2calc with a breakpoint, and then saw the value vanish. So I followed the code.

javascript parseFloat bug

I am subtracting multiple textboxes using JavaScript. I'm adding the total amount in the textboxes.When amount is first entered it works fine but when I change the amount and save it again there is a bug.The value is pulled from database the second time and is in "00.00" format.
The problem is when the value is a 4 digit value say "6500.00" it changes to "6.00". It shows as 6500.00 in the webpage but during calculation in JavaScript it calculates as "6.00".it works fine for 3 digit value like "400.50".Can somebody tell me what's the problem here.
JavaScript:
function fill_balance()
{
var total = document.getElementById("total").value;
var payment = document.getElementById("payment").value;
document.getElementById("balance").value = parseFloat(total) - parseFloat(payment);
}
P.S: I use number_format() function to change the format to 00.00

Categories