Question

How to get value of single line of text into a calculated value

  • 29 February 2024
  • 1 reply
  • 40 views

Badge

Hi There,

may i know how do we get the value of single line of text which has the JavaScript client ID of resultText and calculated value’s ID is calcResult. However the line of $("#" + "calcResult").val(minValue); However, it does not solves the issue. Is there any suggestions?

function calculatelowest() {
    var stringValue = NWF$("#" + jsarrayinteger).val();
    if (stringValue.trim() === "") {
        alert("Input is empty. Cannot calculate CPI Score");
        return;
    }
    var numberArray = stringValue.split(',').map(Number);
    if (numberArray.some(isNaN)) {
        alert("Invalid input. Please provide a valid list of numbers separated by commas.");
        return;
    }
    var minValue = Math.min(...numberArray);
//    alert(minValue);
NWF$('#' + resultText).val(minValue);
$("#" + "calcResult").val(minValue);
}


1 reply

Userlevel 5
Badge +14

The reason you’re having this issue is because Calculated Controls are not using exposed Input elements like Single Line Text Controls. You can not simply update them using the jQuery.val() method. However, Calculated Control Formulas are evaluated JavaScript, so doing complex calculations inside of them is trivial.

 

Here is a simple example

 

I have a form with controls. A Single Line Text control named “control_JSArrayIntegers” and a Calculated Control named “control_CalcResult”:

 

 

 

The Formula for my Calculated Control is as follows:

(function(controlCall){

var returnValue = "";
var formControlID = controlCall.split("'")[1] || "";
var targetControl = sourceContext.find("[formcontrolid='" + formControlID + "'].nf-filler-control");
var inputValue = targetControl.find("[formcontrolid='" + formControlID + "'].nf-associated-control").val();


var minValue = Math.min (...(inputValue.split(",").filter(function(s){return s;}).map(Number)));

if (Number.isFinite(minValue)) {
returnValue = minValue;
}

return returnValue;
}("<<<{control_JSArrayIntegers}>>>"))

 

NOTE: You will need to replace the very bottom “<<<{control_JSArrayIntegers}>>>” with an ACTUAL reference to the control that will act as the Input for this calculation! BUT it will need to be between quotes still as shown in this image!!:

 

I have to do it this way because right now Nintex Forms will return a Numeric Value if the user inputs something like “12,33,43” instead of “12, 33, 43” (with spaces)!

 

Running the form I can get the answers expected

 

With no spaces after commas:

 

With spaces after commas:

 

With invalid entries:

 

Furthermore this way you can place your validation logic in an actual Validation Rule instead of making a popup for the user to see (or you can add your own code to do a popup! I won’t stop you!)
 

If you really want to get funky you can even add a strange little rule to the validation of the Single Line Text control to change the Error Message based on what’s invalid:

 

(copy and paste the below code into the formula dialog)

(function(rowIndex){
var controlValue = NWF$("#" + rowIndex).val();
var callerName = arguments?.callee?.caller?.name ?? "";

var validatingSpan = NWF$("#" + rowIndex).parent().siblings("span").filter (function(index, element){
return NWF$(element).prop ("functionName") === callerName;
});

if (controlValue.trim () === "") {
validatingSpan.prop("errormessage", "You cannot leave this field blank!");
return true;
} else if (
(controlValue.split(",").filter(function(s) {
return s;
}).map(Number)).some(function(n){
return !Number.isFinite(n);
})) {
validatingSpan.prop("errormessage", "Your string cannot contain non-numeric entries!");
return true;
}

return false;
}(rowIndex));

 

When trying to save with no input:

 

When trying to save with bad inputs:

 

Let me know if that solves your issue

 

 

Reply