Array and form question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    Array and form question

    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]
  • Ron

    #2
    Re: Array and form question

    Sandman wrote:
    [color=blue]
    >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?
    >
    >
    >[/color]
    I would use two corresponding arrays. Add an
    onblur="fillOut Form(this.value )" attribute to your email element. The
    fillOutForm() function could look like this:

    function fillOutForm(ema ilAddress) {
    var name = "";
    for(i=0;i<email Array.length;i+ +) {
    if(emailAddress ==emailArray[i]) {
    name = nameArray[i];
    }
    }
    document.getEle mentById("nameF ield").value = name;
    }

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

    function fillOutForm(ema ilAddress) {
    var name = "";
    for(i=0;i<email Array.length;i+ +) {
    var pair=emailArray[i].split("=", 2);
    if(emailAddress ==pair[0]) {
    name = pair[1];
    }
    }
    document.getEle mentById("nameF ield").value = name;
    }

    Comment

    • Ron

      #3
      Re: Array and form question

      Sandman wrote:
      [color=blue]
      >How would you solve it?
      >
      >[/color]
      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...
      }

      Comment

      • lallous

        #4
        Re: Array and form question

        Hello,

        Use objects or 2 parallel arrays.

        Here is an example of objects:

        <script language="JavaS cript">
        <!--
        var x = new Object;
        x["user1@yahoo.co m"] ="User #1";
        x["user2@yahoo.co m"] ="User #2";
        x["user3@yahoo.co m"] ="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-379F47.10475606 052004@individu al.net...[color=blue]
        > Hello - I have a little challenge here which I haven't been able to solve,[/color]
        and[color=blue]
        > thought you might help me.
        >
        > I have a "Register a new member" page for groups of people. There is a[/color]
        group[color=blue]
        > leader that signs up new members to the group. I also have a member[/color]
        database[color=blue]
        > which is unrelated, but could be of help.
        >
        > Basically, the group leader should be able to add any person, but the[/color]
        system[color=blue]
        > 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,[/color]
        javascript[color=blue]
        > will iterate through an array I've created beforehand of known email[/color]
        addresses[color=blue]
        > and their corresponding names, and thus auto-filling the Name-field. Do[/color]
        you[color=blue]
        > understand?
        >
        > How would you solve it?
        >
        > --
        > Sandman[.net][/color]


        Comment

        Working...