4 Replies Latest reply on Apr 17, 2015 10:53 AM by Onsightclimb

    Validate text field value

    Onsightclimb

      Hello.

       

      I found this code:

       

      //set the vars

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

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

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

       

       

      //this function concatenates two field values into one with 'and' as a separator.

      function concatFields(field1,field2,fieldDest){

      //ensure all vars are present

      if(field1 && field2 && fieldDest){

        var fieldVals = new Array();

        if(field1.value!='' && field1.value!=null){

         fieldVals.push(field1.value);

        }

        if(field2.value!='' && field2.value!=null){

         fieldVals.push(field2.value);

        }

        fieldDestResult = fieldVals.join(' and ');

        //only populate the fieldDest if it's empty

        //if(fieldDestResult.value!=''||fieldDestResult.value!=null){

         fieldDest.value=fieldDestResult;

        //}

      }

      }

      concatFields(one,two,three);

       

      This combine values of two text fields into one (text field nr.3) and puts "and" between values. It works OK. But what if both values are equal? Then I need that only one value is populate in field nr.3. So..if values in field "Name1" and "Name2" are different, both are combined in "Name3", ...if values are equal, only one of them will be send to field "Name3".

      How can I do that?

       

      Thank you for answer.

        • 1. Re: Validate text field value
          gkaiseril MVP & Adobe Community Professional

          Lots of people post code. I have a different approach to conditional concatenation  that can process up to 3 string values and use and optional separator and adjust for any null strings.

          So I would use a custom  JavaScript calculation script in the field for  like:

          // document level funciton;
          // Concatenate 3 strings with separators where needed
          function fillin(s1, s2, s3, sep) {

          /*
          Purpose: concatenate up to 3 strings with an optional separator

          inputs:
          s1: required input string text or empty string
          s2: required input string text or empty string
          s3: required input string text or empty string
          sep: optional separator sting

          returns:
          sResult concatenated string
          */
          // variable to determine how to concatenate the strings
            var test = 0; // all strings null
            var sResult; // re slut string to return
          // force any number string to a character string for input variables
            s1 = s1.toString();
            s2 = s2.toString();
            s3 = s3.toString();
            if(sep.toString() == undefined) sep = ''; // if sep is undefined force to null
          /*
          assign a binary value for each string present 
          so the computed value of the strings will indicate which strings are present
          when converted to a binary value
          */
            if (s1 != "") test += 1; // string 1 present add binary value: 001
            if (s2 != "") test += 2; // string 2 present add binary value: 010
            if (s3 != "") test += 4; // string 3 present add binary value: 100
            /* return appropriate string combination based on
            calculated test value as a binary value
            */
            switch (test.toString(2)) {
            case "0": // no non-empty strings passed - binary 0
               sResult = "";
            break;
            case "1": // only string 1 present - binary 1
               sResult = s1;  
            break;
            case "10": // only string 2 present - binary 10
               sResult = s2;  
            break;
            case "11": // string 1 and 2 present - binary 10 + 1
               sResult = s1 + sep + s2; 
            break;
            case "100": // only string 3 present - binary 100
               sResult = s3;
            break;
            case "101": // string 1 and 3 - binary 100 + 001
               sResult = s1 + sep + s3; 
            break;
            case "110": // string 2 and 3 - binary 100 + 010
               sResult = s2 + sep + s3; 
            break;
            case "111": // all 3 strings  - binary 100 + 010 + 001
               sResult = s1 + sep + s2 + sep + s3; 
            break;
            default: // any missed combinations
               sResult = "";
            break;
          }
          return sResult;
          }
          // end document level function fillin;


          var Text1 = this.getField("Name1"),valueAsString;
          var Text2 = this.getField("Name2"),valueAsString;
          if(Text1 == Text2) Text2 = "";
          event.value = fillin(Text1, Text2, ""; " and ");

          • 2. Re: Validate text field value
            Onsightclimb Level 1

            I try to use your solution but I get SyntaxError missing ) after argument list 69 after line 70.

            I m not programmer, can you help me. Must I put this code in field, where I want to display results or I must put code on document javascript?

            • 3. Re: Validate text field value
              gkaiseril MVP & Adobe Community Professional

              There is is coma were a period should be. The last four lines soul be:

               

              var Text1 = this.getField("Name1").valueAsString;

              var Text2 = this.getField("Name2").valueAsString;

              if(Text1 == Text2) Text2 = "";

              event.value = fillin(Text1, Text2, "", " and ");

               

              Additional code could be added to remove case sensitivity and trim leading or trailing white spaces.

              • 4. Re: Validate text field value
                Onsightclimb Level 1

                Thank you, guys. Awesome!!