Array and form question

S

Sandman

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?
 
R

Ron

Sandman said:
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) {
name = nameArray;
}
}
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.split("=", 2);
if(emailAddress==pair[0]) {
name = pair[1];
}
}
document.getElementById("nameField").value = name;
}
 
L

lallous

Hello,

Use objects or 2 parallel arrays.

Here is an example of objects:

<script language="JavaScript">
<!--
var x = new Object;
x["(e-mail address removed)"] ="User #1";
x["(e-mail address removed)"] ="User #2";
x["(e-mail address removed)"] ="User #3";

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

This sample shows how to iterate/add elements.

HTH
Elias
 
R

Ron

Sandman said:
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...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top