7 Replies Latest reply on Jul 11, 2008 12:30 AM by Loic.Aigon

    JS field validation

    John.Kordas Level 1
      This should be easy but Im missing something.

      Using the simple Hello World example I want an alert to pop up if the content field is empty. Then display the dialog again to the user so they type something into the filed.

      The changes:

      //Create a text edit field.
      var myTextEditField = textEditboxes.add({minWidth:180});

      var myResult = myDialog.show();

      if(myTextEditField.editContents != ""){
      if(myResult == true ){
      //Get the values from the dialog box controls.
      var myString = myTextEditField.editContents;


      }else{
      //User clicked Cancel, so remove the dialog box from memory.
      myDialog.destroy(); }
      }else{
      alert("Please enter some text into the contents field.");
      }

      Now I get the alert but the dialog closes as well. Is it possible to show the alter but not close the dialog?
        • 1. Re: JS field validation
          Harbs. Level 6
          Use a self-calling function to show the dialog again if the field is empty.


          Harbs
          • 2. Re: JS field validation
            John.Kordas Level 1
            Thanks Harbs,

            Do you mean somthing like:

            }else{
            alert("Please enter some text into the contents field.");
            myDialog.show();
            }

            I tried this but it only worked once then the dialog closed on the third click of the ok button.
            • 3. Re: JS field validation
              Loic.Aigon Adobe Community Professional
              function mydialog()
              {
              var mydialog = app.dialogs.add();
              //your dialog creation script
              if(mydialog.show()!= null)
              {
              if(myTextEditField.editContents == "")
              {
              mydialog();
              }
              else
              {
              // doSomethingHere();
              }
              }
              }
              function doSomethingHere()
              {
              //do something;
              }

              mydialog();
              • 4. Re: JS field validation
                John.Kordas Level 1
                Thanks Ioic_aigon,

                working through what you have I get a error Object is undefined:

                function mydialog()
                {
                var mydialog = app.dialogs.add();
                //your dialog creation script
                with(mydialog){
                //Add a dialog column.
                with(dialogColumns.add()){
                //Create a text edit field.
                //var myTextEditField = textEditboxes.add({editContents:"Hello World!",minWidth:180});
                var myTextEditField = textEditboxes.add({minWidth:180});
                //Create a number (real) entry field.
                var myPointSizeField = realEditboxes.add({editValue:72});
                }
                }
                if(mydialog.show()!= null)
                {
                if(myTextEditField.editContents == "")
                {
                alert("Please enter some text into the contents field.");

                b mydialog(); <==== Object is undefined
                }
                else
                {
                // doSomethingHere();
                }
                }
                }
                function doSomethingHere()
                {
                //do something;
                }

                mydialog();
                • 5. Re: JS field validation
                  John.Kordas Level 1
                  This is also most working it will bring the dialog back up after you click the ok on the alert. The only problem now is the Cancel will not work unless you enter somthing in the text field.

                  function validate(){

                  var myDialog = app.dialogs.add({name:"Simple User Interface Example Script",canCancel:true});
                  with(myDialog){
                  //Add a dialog column.
                  with(dialogColumns.add()){
                  //Create a text edit field.
                  //var myTextEditField = textEditboxes.add({editContents:"Hello World!",minWidth:180});
                  var myTextEditField = textEditboxes.add({minWidth:180});
                  //Create a number (real) entry field.
                  var myPointSizeField = realEditboxes.add({editValue:72});
                  }
                  }
                  //Display the dialog box.

                  var myResult = myDialog.show();
                  if(myTextEditField.editContents == "")
                  {
                  alert("Please enter some text into the contents field.");
                  validate();
                  }else{
                  if(myResult == true){
                  //Get the values from the dialog box controls.
                  var myString = myTextEditField.editContents;
                  var myPointSize = myPointSizeField.editValue;
                  //Remove the dialog box from memory.
                  myDialog.destroy();
                  //Create a new document.
                  var myDocument = app.documents.add()
                  }
                  else{
                  //User clicked Cancel, so remove the dialog box from memory.
                  myDialog.destroy();
                  }
                  }
                  }
                  validate();
                  • 6. Re: JS field validation
                    John.Kordas Level 1
                    Ok to get he above code to work I changed:

                    if(myTextEditField.editContents == "")

                    to

                    if(myResult == true && myTextEditField.editContents == "")

                    If anyone can see anything wrong with the way I've gone about it please leat me know.

                    Cheers.
                    • 7. Re: JS field validation
                      Loic.Aigon Adobe Community Professional
                      Hi John,
                      The script is on the way !
                      You if con,dition may be simplier :
                      if(myTextEditField.editContents == "") //entry is ""
                      {
                      alert("Please enter some text into the contents field.");
                      validate();
                      }
                      else if(myResult == true) //user entered datas
                      {
                      //action here
                      }
                      else //implies user clicked cancel
                      {
                      myDialog.destroy();
                      }