Skip to main content
Known Participant
June 18, 2015
Answered

Validate a edit text field

  • June 18, 2015
  • 6 replies
  • 938 views

Hi

I have created a simple panel with a username and password as statictext and a pair of edit text and a go button to work as a "enter' button for the edit text.

So what I am trying to achieve, is having a "if/else" statement validate the text.

so i created 2 variables, one for username and one for password.

Exsample:

var userName = "Test1234"

As you see here below, you can see the group structure of the text, edit txt and button.

  groupOne: Group{orientation:'column' , alignment:['fill','fill'], alignChildren:['fill','fill'],\

            myStaticTxt: StaticText {text: 'Username:'},\

            myStaticTxt2: StaticText {text: 'Password:'},\

            },\

            groupTwo: Group{orientation:'column' , alignment:['fill','fill'], alignChildren:['fill','fill'],\

            myEditTxt: EditText {text: ''},\

            myEditTxt2: EditText {text: ''},\

            },\

            groupThree: Group{orientation:'column' , alignment:['fill','fill'], alignChildren:['fill','fill'],\

            mygoBTN: Button {text: 'Go'},\

When the user has written a text in the edit text field for username, I want to validate it like below.

var goBTN = myPanel.grp.groupThree.mygoBTN;

   

    goBTN.onClick = function(){

   

        if(myEditTxt == "Test1234"){

           

            alert("Acces Granted");           

           

            }else{

                alert("Sorry, try again");

               

                }

       

        }



But nothing happends, what am I missing here.

Also is it possible for the password textfield to have hidden characters??

Cheers

/HRB



This topic has been closed for replies.
Correct answer Dan Ebberts

I think it would be more like this:

if(myPanel.grp.groupTwo.myEditTxt.text == "Test1234"){



Dan

6 replies

hrohibilAuthor
Known Participant
June 23, 2015

Thanks... That did it...

hrohibilAuthor
Known Participant
June 21, 2015

Hi

I tried move it up as you said like so :

  verifyBTN.onClick = function(){

                 

                  if(myPanel.grp.groupTwo.myEditTxt1.text == null || myPanel.grp.groupTwo.myEditTxt2.text == null){

                      alert("Please enter credentials");

                     

                      }else if(myPanel.grp.groupTwo.myEditTxt1.text == usrName && myPanel.grp.groupTwo.myEditTxt2.text  == passWord){

             

                  alert("Access granted");

               

                var myTotalItems = 70000;

                 

              myStaticText.visible = true; 

            progressBar.visible = true;         

                 

                    for(var i=0; i <= myTotalItems; i++ ){

        percentage = Math.floor((i/myTotalItems)*100);

        myStaticText.text = percentage.toString() + "%";

        progressBar.value++; 

       

            }                         

                         

                          if( percentage == 100){

                              myTest.visible = true;

                             

                              }

     

                     

                  }else{

                              alert("Sorry try again");

                             

                           

                                 

                                    }

                     

                        }

I mean, the  when not entering anything and hit the verify button, the alert says "sorry try again", instead of "Please enter credentials"  like if nothing has been entered. If i use correct username and password then the code also works with the access granted and starts the loop. But i cant seem to have a action when the fields are empty..

cheers

Legend
June 23, 2015

For the top most if statement you only need to check once, no need for an "else if" level. One thing I did forget to mention was that "null" doesn't match a blank edittext box, so either adding two more checks (commented out below) or simply changing the "null" to "" will catch the blank edittext boxes.

goBTN.onClick = function(){

     //if(myEditTxt1.text == null || myEditTxt2.text == null || myEditTxt1.text == "" || myEditTxt2.text == ""){     //Checks for null and blank

     if(myEditTxt1.text == "" || myEditTxt2.text == ""){     //Checks for blank only

          alert("Please enter credentials");

     }else{     //If the boxes are not blank, run the rest of code as before.

          var myObjLen = myObj.user.length;

          var locked = true;

          for(var i=0; i<myObjLen; i++){

               if(myEditTxt1.text == myObj.user.un && myEditTxt2.text == myObj.user.pass){

                    alert("Acces Granted");

                    locked = false;

                    break;

               }

          }

          if(locked == true){

               alert("Sorry, try again");

          }

     }

}

hrohibilAuthor
Known Participant
June 21, 2015

Ahh. Ok that makes sence. I will try later when I get to a pc...

Thanks

Cheers

Hamid

hrohibilAuthor
Known Participant
June 20, 2015

Hi David

Ok, I figured out the red error line in the code. It was simply formatting issue and a couple of empty space that had to be deleted.

But to above script I have 2 questions.

1:  

In the code line between 1 and 7 The 2 alerts as showen below:

  1.                      alert("Acces Granted"); 
  2.                           locked = false; //Convert locked variable to false 
  3.                           break
  4.                       } 
  5.                 } 
  6.                 if(locked == true){ //Checks if locked variable is true 
  7.                       alert("Sorry, try again"); 
  8.                 } 
  9.             } 
  10.  

    I have tried to add another if statment, to check if the user has not entered anything so like a null/blank,and hits the GO button.

                      if(myEditTxt.text == null && myEditTxt2.text == null){

                    alert("Please enter credentials");

}

But this does not alter anything, if I hit the go button with that added code, then it skip to the else statement and alerts sorry...

2: My second question is, cant the GO button be linked to a ENTER key on the keyboard, so that when i have written in my fields, I hit enter on the keyboard and that should function as the same as if i hit the GO button??

Enjoy your weekend...

Cheers

Hamid

Legend
June 20, 2015
But this does not alter anything, if I hit the go button with that added code, then it skip to the else statement and alerts sorry...

Currently...

if(myEditTxt.text == null && myEditTxt2.text == null){


...is checking to see that BOTH entry boxes are null at the same time. In reality there may be one box empty or both. So instead of...

if(myEditTxt.text == null && myEditTxt2.text == null){

...use...

if(myEditTxt.text == null || myEditTxt2.text == null){

Also you would want to place the empty edittext check as a top level check first...

if(myEditTxt.text == null || myEditTxt2.text == null){ //Use a double pipe symbol to check if one is empty OR 2 is empty

    //Run the original checks inside here

}else{

    alert("Please enter credentials");

}

Legend
June 18, 2015

Here is an adjusted example inside of full dockable/floating code so it can be tested. I adjusted the layout for the statictext and edittext elements so they are side-by-side. I changed up the onClick to include a loop for easier checking of user credentials. Now this is by no means a secure setup at all since the user credentials are clearly setup in plain text right at the top in line #4, but I used this setup just to show how the Go button onClick loop can access the user credentials. Again this not a secure login code setup at all, just a simple explanation of checking for username and password via a basic loop and object array containing user data. Also there is a comment on line #16 referring to the noecho creation property of an edittext element. Noecho will change text over to the * character for hidden password style entry.

function loginScript(thisObj){

  var myObj = new Object();

  myObj.user = new Array({'un':"Test1234", 'pass':"password"}, {'un':"Tommy", 'pass':"12345"}); //!!!!! WARNING !!!!! NOT A GOOD IDEA TO HAVE USER CREDENTIALS OUT IN THE OPEN LIKE THIS!!!!

  function loginScript_buildUI(thisObj){

      var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "TEST", undefined, {resizeable:true});

      if (myPanel != null){

            var res ="group {orientation:'column', alignment:['fill','fill'], alignChildren:['fill','top'],\

                groupOne: Group{orientation:'row' , alignment:['fill','top'], alignChildren:['fill','top'],\

                      myStaticTxt: StaticText {text: 'Username:', alignment:['left','fill']},\

                      myEditTxt: EditText {text: ''},\

                },\

                groupTwo: Group{orientation:'row' , alignment:['fill','top'], alignChildren:['fill','top'],\

                      myStaticTxt2: StaticText {text: 'Password:', alignment:['left','fill']},\

                      myEditTxt2: EditText {text: '', properties:{noecho:true}},\     //noecho creation property obscures the text and uses * character instead

                },\

                groupThree: Group{orientation:'column' , alignment:['fill','top'], alignChildren:['fill','top'],\

                      mygoBTN: Button {text: 'Go'},\

                },\

            }";

            myPanel.grp = myPanel.add(res);

            var myEditTxt = myPanel.grp.groupOne.myEditTxt;

            var myEditTxt2 = myPanel.grp.groupTwo.myEditTxt2;

            var goBTN = myPanel.grp.groupThree.mygoBTN;

            goBTN.onClick = function(){

                var myObjLen = myObj.user.length;

                var locked = true; //Start locked variable as true

                for(var i=0; i<myObjLen; i++){ //Loops through user credentials array

                      if(myEditTxt.text == myObj.user.un && myEditTxt2.text == myObj.user.pass){ //Verifies Username and password match

                          alert("Acces Granted");

                          locked = false; //Convert locked variable to false

                          break;

                      }

                }

                if(locked == true){ //Checks if locked variable is true

                      alert("Sorry, try again");

                }

            }

            myPanel.layout.layout(true);

            myPanel.grp.minimumSize = myPanel.grp.size;

            myPanel.layout.resize();

            myPanel.onResizing = myPanel.onResize = function () {this.layout.resize();}

      }

      return myPanel;

  }

  var rPal = loginScript_buildUI(thisObj);

  if (rPal != null){

      if (rPal instanceof Window){

          rPal.center();

          rPal.show();

      }

  }

}

loginScript(this);

hrohibilAuthor
Known Participant
June 18, 2015

Hi David.

I was allready using a panel, I created by going through your amazing extendscript series...

I get a error on line 9. It just displays red, it does not display error message...

Best regards

(Thank you so much for the series. I am learning so much..)

Legend
June 18, 2015
I get a error on line 9. It just displays red, it does not display error message...

Are you in AE CC 2014 or 2015?

Update:

I just tried this code in CC 2014 and it worked fine. No error or red window.

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
June 18, 2015

I think it would be more like this:

if(myPanel.grp.groupTwo.myEditTxt.text == "Test1234"){



Dan