-
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
-
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.
-
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..
-
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.
-
O, yes it works! Tried it! Thats right, just inserted break to exit the loop.
Thanks for quick reply jamesloacher!!
-
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|