5 Replies Latest reply on Oct 31, 2014 9:18 AM by gkaiseril

    Field Validation

    BecSimpson1

      Hi,

       

      I have a form where two fields are to be compared. Both should be the same and if not there should be an explanation as to why. The fields contain both text and numerals.

       

      Would someone be able to help me write the validation script to ensure an alert pops up when there is a discrepancy?

       

      Thanks

        • 1. Re: Field Validation
          MichaelN Level 3

          Assuming your fields are called something like "Text1" and "Text2", make this the custom calculation script for Text2:

           

           

               //Check if fields are the same
               //Make this the custom calculation script for Text2

               var f = this.getField("Text1").value;
               var g = this.getField("Text2").value;

               if (f !== g){
               app.alert("Fields are not the same", 3);
               }

           

          Just replace "Text1" and "Text2" with the names of the fields in your form.

          • 2. Re: Field Validation
            try67 MVP & Adobe Community Professional

            That's not quite sufficient.

             

            If you use a validation script it needs to be placed in both fields, and

            then you should change the field name where the code is located with

            event.value.

            • 3. Re: Field Validation
              gkaiseril MVP & Adobe Community Professional

              You should also allow for the situation when on or both fields are null strings, do not have an entry, or the validation will indicate a failure when there has been data entered into one of the fields but not the other. You would also need to allow for being within one of the field when running the script and if one uses the "the.getFeild().value" for the field with the focus the value at the time focus on the field occurred will be used and not the value entered into the field.

               

              // document level script for comparison of 2 fields
              function FieldsEqual(cFieldName1, cFieldName2) {
              var bValidation = true;
              if(event.target.name == cFieldName1){
              cField1 = event.value;
              cField2 = this.getField(cFieldName2).value;
              }

              if(event.target.name == cFieldName2){
              cField1 = event.value;
              cField2 = this.getField(cFieldName2).value;
              }

              if(cField1 != "" && cField2 != ""){
              // fields are not empty;
              if (cField1 != cField2) {
              // fields are not equal
              app.alert("Fields are not the same", 1, 0); // alert
              bValidation = false; // set event.rc for failed validation;
              } // end fields not equal
              } // end fields not null
              return bValidation; // return status of comparison;
              } // end 2FieldsEqual funciton;
              // end of doucment level function comparing 2 fields


              // Custom validation script "Text1" field;
              // set validation return code based on comparison;
              event.rc = FieldsEqual("Text1", "Text2");
              // end custom validation script Text1

               

              // Custom validation script "Text2" field;

              // set validation return code based on comparison;

              event.rc = FieldsEqual("Text1", "Text2");

              // end custom validation script Text2

              • 4. Re: Field Validation
                KDRamsey

                How would you do the custom validation scripts when the two fields being compared are calculated? Example: field1 is the product of three entries and field2 is the sum of 15 entries. The results of the two calculated fields should be compared, and an alert displayed if they do not match.

                 

                Thank you.

                • 5. Re: Field Validation
                  gkaiseril MVP & Adobe Community Professional

                  You might want to start a new discussion since this is a little different than the original post.

                   

                  You need to design your code so it only runs when the other 2 fields are completed or revised. I would look at using a number of scripts.

                   

                  1. The validation only runs when both field1 and field2 have none null values.

                  2. Write a function to perform the validation using the non-null values of field1 and field2.

                  3 . Calling that function in the validation scripts for field1 and field2.