set a value in a field based on the string length in another field

I have to set a value ("OK") in a form field "TEST" when the text in another field "ATA" has a 4 character length. Tried this js in the Properties>Calculate>Custom calculation script of the "TEST" field:

var cNum = getField("ATA");
if (event.value.length < 4) this.getField("TEST").value="";
else this.getField("TEST").value="OK";

Thanks for the support


Enrico D'Anna


6 Answers

Almost... you got the two fields mixed-up.

if (getField("ATA").value.length < 4) event.value = ";
else event.value="OK";


Gilad D (try67)   

Thanks Gilad, I modified but seems to work with alphabetic characters. Unfortunately the field is filled with numers and the JS gives an error and is always "OK" (else condition). In the consolle:

InvalidSetError: Set not possible, invalid or unknown.
Field.value:5:Field ATA:Validate

Any idea?


Enrico D'Anna   

Numeric values are not stored as strings like alphabetic characters are but in a floating point number. You need to cover the numeric entry into a string value.

var cNumValue = this.getField("ATA").vlaue.;
var cNumString = cNumValue.toString()
if (cNumString < 4) event.value = "";
else event.value = "OK";

If you want to include leading zeros if entered then you would use the "valueAsString" property.

var cNumString = this.getField("ATA").vlaueAsString.;
if (cNumString < 4) event.value = "";
else event.value = "OK";


George Kaiser   

I tried both codes, but now the field is no more working for alphabetic characters and is instead monitoring the numeric value (if I typed 4 is "OK", while 3 is ""), not the length. The field "ATA" must contain 4 numeric characters (0000, 2500, 3453, etc) and I placed 2 controls: the first is the maximum number of characters (4), the second is a JS in the ATA field (Validate TAB) to avoid a code of less than 4 characters:

var cNum = getField("ATA");
if (event.value.length < 4) {var msg = "Please enter 4 characters for the ATA!";
app.alert(msg, 1, 0, "Character Limit Error");
event.target.fillColor = color.red;
cNum.value = "";}
else {event.target.fillColor = color.transparent;
cNum.value = event.value;
}

Now I need to populate this additional field "TEST" when the data in "ATA" is correct, but the numeric format seems to be a problem ...


Enrico D'Anna   

You're placing it in the wrong place. The script above should be the calculation script of the TEST field, not the validation of ATA.


Gilad D (try67)   

To be honest the JS that color the field "ATA" and pop-up the warning works fine in the validate of the "ATA" (and doesn't work in the "TEST" calculation - I tried after Your suggestion). That JS is not the problem. The problem is the other JS in the "TEST" calculation that doesn't work. To summarize there are 2 fields "ATA" and "TEST". The JS that gives the warning is working and is placed in the Validate of "ATA", the JS that should fill the "TEST" is not working and is in the Calculate of "TEST"
Tell me if this is more clear ...


Enrico D'Anna   


Please specify a reason: