PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Login Form Help as3
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Login Form Help as3

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    28

    Login Form Help as3

    I have two arrays of strings
    containing usernames and passwords for login,
    Code:
    var uN:Array;
    var pW:Array;
    uN = ["fla","fli","flu"];
    pW = ["adv","advi","advu"];
    I need to do a function which will compare the positions of certain string from one array
    with position of string from another.And get something like
    Code:
    if(username.text == uN[0] && password.text == pW[0]){
            logIn();
        }
    So "fla" is match with "adv".
    How can I infact mach other usernames with passwords to allow login, without doing a row of "if"
    statements for each string to match appropriate one?
    Thanx

  2. #2
    Senior Member
    Join Date
    Jan 2006
    Posts
    263
    You can loop through the uN array a capture the position, then test the pW position.

    for( var i:int = 0; i < uN.length;i++){
    if(uN[i] == username.text){
    if(pW[i] == password.text){
    logIn();
    }
    }
    //Else username and(or) password is wrong
    }

    Not tested but should work.
    Last edited by jamesloacher; 10-22-2011 at 01:32 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    28
    Will that mach a username with correct pass? I dont want to login as a user with just any pass from array.. Havent tried Your example, thats why Im asking..

  4. #4
    Senior Member
    Join Date
    Jan 2006
    Posts
    263
    Yes it should work just fine for matching the username with password.

    You can throw a break in after it matches one.

    Actionscript Code:
    for( var i:int = 0; i < uN.length;i++){
    if(uN[i] == username.text){
    if(pW[i] == password.text){
    logIn();
    break;
    }

    }
    //Else username and(or) password is wrong
    }
    Last edited by jamesloacher; 10-22-2011 at 01:46 PM.

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    28
    O, yes it works! Tried it! Thats right, just inserted break to exit the loop.

    Thanks for quick reply jamesloacher!!

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I question the wisdom of doing login checks on the client, but you can do what James suggests in a more compact way.
    Code:
    var pos:int = uN.indexOf(username.text);
    if ((pos != -1) && (pW[pos] == password.text)){
      logIn();
    }else{
      fail();
    }
    Or, even more compactly:
    Code:
    pW[uN.indexOf(userName.text)] == password.text ? login() : failLogin();
    Of course, I wouldn't use parallel arrays even if I were doing something like this client side. It's better to have an object with both the username and password and check that the password for that object matches the username for that object. You can also use an Object as a hashmap to look things up by String, so you can get the passwords or user objects by username rather than have to iterate through a list.

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    28
    Thanks mr 5Tons, for optimized code, but much more for advice on how to do stuff. You guys are amazing!
    Btw, why is there some stupid dot flashing under my input cursor, in input field??
    It appears only when I play swf outside of flash, inside IDE is ok..?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center