Expand my Community achievements bar.

alert message pop up twice in IF statement

Avatar

Level 5

I'm trying create function to validate with limit login attempt. That is my code:

var attempt = 3; // Variable to count number of attempts.

function validate() {

var username = form1.page1.subLogin.txtUserName.rawValue;

var password = form1.page1.subLogin.txtPassword.rawValue;

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

if (username == _Username[i] && password == _Password[i]) {

    app.alert("Login successfully");

}

}

if (attempt <= 3) {

app.alert(String.concat("Invalid username and/or password.  You have ",  attempt, " left."));

attempt --;

if (attempt == 0) {

    app.alert("You do not have authentication for that mode.");

    app.setTimeOut("this.closeDoc(true)", 2000);

}

}

}

where _Username and _Password are global variables array which hold usernames and according passwords of the users.

The problem in the second IF statement. When username or/and password wrong I'm getting twice alert message Invalid username and/or password. The second alert indicate decries number of attempt. How to fix the problem and get alert message one by one in case user typed wrong username and/or password?

Thanks.

5 Replies

Avatar

Level 10

Hi,

Try swapping the order of the if statements around, so

if (attempt == 0) {

  app.alert("You do not have authentication for that mode.");

  app.setTimeOut("this.closeDoc(true)", 2000);

} else {

  if (attempt <= 3) {

   app.alert(String.concat("Invalid username and/or password.  You have ", attempt, " left."));

   attempt--;

  }

}

Is this code in a script object? and how are _Username and _Password declared, you have to be carefull that these don't get removed by the garbage collector

Avatar

Level 5

Hi BR001. Thanks foe replay.

I tried your suggestion code but unfortunately when I typed wrong password and/or username I got alert:

"Invalid username and/or password.  You have 2 left."

After I click OK on alert message I got alert:

"Invalid username and/or password.  You have 1 left.

How to fix the problem?

Thanks.

Avatar

Level 10

This sounds like a problem with the way the validate function is being called as the alerts are outside of the loop.  Can you explain how it is called and show the code and/or post a link to your form.

Avatar

Level 5

Hi BR001.

That is design of Login form:

LoginForm.png

That is Hierarchy of the form:

FormHierarchy.png

Where Login is Script Object of page1.

The complete code of Login script object in my first post.

The code of button btnLogin on click event: Login.validate();

The code of text field txtPassword on exit event: Login.validate();

That is complete description of Login form.

Thanks.

Avatar

Level 10

Here's a sample form with the code I suggested in it.

https://sites.google.com/site/livecycledesignercookbooks/home/eugzl.pdf?attredirects=0&d=1

There's a second sample in the form, just shows passing the username and password into the validate function as arguments, making it more self-contained.  And, showing with a return statement once the valid username/password are entered.

Hope this helps

Bruce