6 Replies Latest reply on May 7, 2013 9:37 AM by MarkWalsh

    Send alert if certain text is inserted in a text field

    Pengwho

      Hi everyone,

       

      I am a total noob on javascript and with your permission I'd ask for a solution on this.

      I need help on creating validation script in this scenario:

      If the content inserted in a text field is "apple", then send an alert "it's red", if the content inserted is "banana", then send "it's yellow". If anything else is inserted then send an alert "I have no idea"

       

      How should i write the validation script?

       

      Thanks a million!

        • 1. Re: Send alert if certain text is inserted in a text field
          try67 MVP & Adobe Community Professional

          if (event.value=="apple") app.alert("It's red!",3);

          else if (event.value=="banana") app.alert("It's yellow!",3);

          else if (event.value!="") app.alert("I have no idea.",3);

          • 2. Re: Send alert if certain text is inserted in a text field
            Pengwho Level 1

            Thanks a lot for your time and answer. Works as a charm!

             

            in case that the scenario changes to:

            If the content inserted in a text field is "apple" or "strawberry" or "cherry", then send an alert "it's red", if the content inserted is "banana" or "mango", then send "it's yellow". If anything else is inserted then send an alert "I have no idea"

             

            how should i put it?

             

            still, thanks a lot for your help

            • 3. Re: Send alert if certain text is inserted in a text field
              try67 MVP & Adobe Community Professional

              if (event.value=="apple" || event.value=="strawberry" || event.value=="cherry") app.alert("It's red!",3);

              else if (event.value=="banana" || event.value=="mango") app.alert("It's yellow!",3);

              else if (event.value!="") app.alert("I have no idea.",3);

              1 person found this helpful
              • 4. Re: Send alert if certain text is inserted in a text field
                MarkWalsh Level 4

                You'd probably be better off using a 'Switch' statement. It would be much easier to read and alter:

                 

                var theValue = event.value.toLowerCase();

                var theMessage = ""

                 

                if (theValue != "") {

                switch (theValue){

                case 'apple':

                case 'strawberry':

                case 'cherry':

                    theMessage = "It's Red!";

                    break;

                case 'banana':

                case 'mango':

                    theMessage = "It's Yellow!";

                    break;

                default:

                    theMessage="I have no Idea?!?";

                }

                 

                app.alert (theMessage)

                }

                1 person found this helpful
                • 5. Re: Send alert if certain text is inserted in a text field
                  Pengwho Level 1

                  Thanks for your reply again. In case the key word is banana for example, but when the "it's a banana" is inserted, instead of just "banana", this script won't work. How should I alter the script to detect a key word, but not the exact word?

                  • 6. Re: Send alert if certain text is inserted in a text field
                    MarkWalsh Level 4

                    Something like this should work, but if the user enters 'It's a banana or apple', it would only show the first value it comes to (it would be Red in this case). If you need it to be able to show both colors, it would need a bit of reworking.

                     

                    var theValue = event.value.toLowerCase();

                    var theMessage = ""

                     

                    if (theValue != "") {

                    switch (true){

                    case theValue.inStr('apple') >= 0:

                    case theValue.inStr('strawberry') >= 0:

                    case theValue.inStr('cherry') >= 0:

                        theMessage = "It's Red!";

                        break;

                    case theValue.inStr('banana') >= 0:

                    case theValue.inStr('mango') >= 0:

                        theMessage = "It's Yellow!";

                        break;

                    default:

                        theMessage="I have no Idea?!?";

                    }

                     

                    app.alert (theMessage)

                    }

                    1 person found this helpful