By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

Array and form question

P: n/a
Hello - I have a little challenge here which I haven't been able to solve, and
thought you might help me.

I have a "Register a new member" page for groups of people. There is a group
leader that signs up new members to the group. I also have a member database
which is unrelated, but could be of help.

Basically, the group leader should be able to add any person, but the system
will later recognize if he hadded someone who is a registered member.

So, the form looks like this:

Email: [ ]
Name: [ ]
[add]

What I would like to do is that when the Email field looses focus, javascript
will iterate through an array I've created beforehand of known email addresses
and their corresponding names, and thus auto-filling the Name-field. Do you
understand?

How would you solve it?

--
Sandman[.net]
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ron
Sandman wrote:
Hello - I have a little challenge here which I haven't been able to solve, and
thought you might help me.

I have a "Register a new member" page for groups of people. There is a group
leader that signs up new members to the group. I also have a member database
which is unrelated, but could be of help.

Basically, the group leader should be able to add any person, but the system
will later recognize if he hadded someone who is a registered member.

So, the form looks like this:

Email: [ ]
Name: [ ]
[add]

What I would like to do is that when the Email field looses focus, javascript
will iterate through an array I've created beforehand of known email addresses
and their corresponding names, and thus auto-filling the Name-field. Do you
understand?

How would you solve it?

I would use two corresponding arrays. Add an
onblur="fillOutForm(this.value)" attribute to your email element. The
fillOutForm() function could look like this:

function fillOutForm(emailAddress) {
var name = "";
for(i=0;i<emailArray.length;i++) {
if(emailAddress==emailArray[i]) {
name = nameArray[i];
}
}
document.getElementById("nameField").value = name;
}

If you wanted to use one array of "emailAddress=name" type entries, you
could use:

function fillOutForm(emailAddress) {
var name = "";
for(i=0;i<emailArray.length;i++) {
var pair=emailArray[i].split("=", 2);
if(emailAddress==pair[0]) {
name = pair[1];
}
}
document.getElementById("nameField").value = name;
}
Jul 23 '05 #2

P: n/a
Ron
Sandman wrote:
How would you solve it?

Sorry, you should probably add a check in the function to make sure that
the user filled in the email address at all before
bothering to iterate through an array. :P

if (emailAddress!="") {
doStuff...
}
Jul 23 '05 #3

P: n/a
Hello,

Use objects or 2 parallel arrays.

Here is an example of objects:

<script language="JavaScript">
<!--
var x = new Object;
x["us***@yahoo.com"] ="User #1";
x["us***@yahoo.com"] ="User #2";
x["us***@yahoo.com"] ="User #3";

for (y in x)
{
alert("email:" + y + " name:" + x[y]);
}
//-->
</script>

This sample shows how to iterate/add elements.

HTH
Elias
"Sandman" <mr@sandman.net> wrote in message
news:mr**********************@individual.net...
Hello - I have a little challenge here which I haven't been able to solve, and thought you might help me.

I have a "Register a new member" page for groups of people. There is a group leader that signs up new members to the group. I also have a member database which is unrelated, but could be of help.

Basically, the group leader should be able to add any person, but the system will later recognize if he hadded someone who is a registered member.

So, the form looks like this:

Email: [ ]
Name: [ ]
[add]

What I would like to do is that when the Email field looses focus, javascript will iterate through an array I've created beforehand of known email addresses and their corresponding names, and thus auto-filling the Name-field. Do you understand?

How would you solve it?

--
Sandman[.net]

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.