Skip to main content
Known Participant
February 4, 2016
Answered

Creating a custom String of text

  • February 4, 2016
  • 1 reply
  • 975 views

I have 4 text boxes. They are Event, City, Email, and String

If Las Vegas is entered in the City text box, I want the String text box to read "City: Las Vegas"

If BK is entered in the Event text box, and Las Vegas is entered in the City text box, I want the String text box to read "Event: BK City: Las Vegas"

If blah@gmail.com is entered in the Email text box, and there is no data in Event, or City text boxes, I want the String text box to read "Email: blah@gmail.com

I can get the data into the String text box, but if any of the 3 text boxes are empty my String text box reads something like this "Event: Email: blah@gmail.com City:"

If any of the Event, City, and/or Email text boxes are empty, I don't want the text associated with the data to show up in the Srtring text box.

I am a novice at javascript. Any help is much appreciated.

This topic has been closed for replies.
Correct answer George_Johnson

I have the javascript on  Button21. In the Properties>Actions Tab Mouse Down trigger, Run Java Script is the action. I hope this helps.


OK. You should use Mouse Up instead, and the script could be changed to:

// Mouse Up script for a button

(function () {

    // Get the field values, as strings

    var sEvent = this.getField("Event").valueAsString;

    var sCity = this.getField("City").valueAsString;

    var sEmail = this.getField("Email").valueAsString;

    // Get a reference to the output field

    var fOutput = this.getField("String");

    var sOutput = "", aOutput = [];  // Initialize output string and array

    // Set the output array

    if (sEvent) {

        aOutput.push("Event: " + sEvent);

    }

    if (sCity) {

        aOutput.push("City: " + sCity);

    }

    if (sEmail) {

        aOutput.push("Email: " + sEmail);

    }

    // Join the elements of the array into a string, separated by a space

    if (aOutput.length > 0) {

        sOutput = aOutput.join(" ");

    }

    // Set the field value to the output string

    fOutput.value = sOutput;

})();

If you want it to happen automatically, you could use the following custom calculation script for the "String" field

// Custom calculation script for String field

(function () {

    // Get the field values, as strings

    var sEvent = this.getField("Event").valueAsString;

    var sCity = this.getField("City").valueAsString;

    var sEmail = this.getField("Email").valueAsString;

   var sOutput = "", aOutput = [];  // Initialize output string and array

    // Set the output array

    if (sEvent) {

        aOutput.push("Event: " + sEvent);

    }

    if (sCity) {

        aOutput.push("City: " + sCity);

    }

    if (sEmail) {

        aOutput.push("Email: " + sEmail);

    }

    // Join the elements of the array into a string, separated by a space

    if (aOutput.length > 0) {

        sOutput = aOutput.join(" ");

    }

    // Set this field's value to the output string

    event.value = sOutput;

})();

1 reply

Inspiring
February 5, 2016

If you post the code you're currently using, we should be able to help.

senechiaAuthor
Known Participant
February 5, 2016

var one = this.getField("Event");

var two = this.getField("City");

var three = this.getField("Email");

var four = this.getField("String");


{four.value="Event: "+one.value+" City: "+two.value+" Email: "+three.value}

Inspiring
February 5, 2016

Ok, one more question. Where is this script located? What is the field name and what event (calculate, validate, etc.)?