<p>What is the best way to refactor the attached code to accommodate multiple email addresses?</p>

<p>The attached HTML/jQuery is complete and works for the first email address. I can setup the other two by copy/pasting and changing the code. But I would like to just refactor the existing code to handle multiple email address fields.</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
    &lt;script src="includes/jquery/jquery-1.2.6.min.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;script language="javascript"&gt;
        $(document).ready(function() {
            var validateUsername = $('#Email_Address_Status_Icon_1');

            $('#Email_Address_1').keyup(function() {
                var t = this;
                if (this.value != this.lastValue) {
                    if (this.timer) clearTimeout(this.timer);
                    validateUsername.removeClass('error').html('Validating Email');

                    this.timer = setTimeout(function() {
                        if (IsEmail(t.value)) {
                            validateUsername.html('Valid Email');
                        } else {
                            validateUsername.html('Not a valid Email');
                        };
                    }, 200);

                    this.lastValue = this.value;
                }
            });
        });

        function IsEmail(email) {
            var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (regex.test(email)) return true;
            else return false;
        }  
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
        &lt;label for="Email_Address_1"&gt;Friend #1&lt;/label&gt;&lt;/div&gt;
        &lt;input type="text" ID="Email_Address_1"&gt;
        &lt;span id="Email_Address_Status_Icon_1"&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for="Email_Address_2"&gt;Friend #2&lt;/label&gt;&lt;/div&gt;
    &lt;input type="text" id="Email_Address_2"&gt;
        &lt;span id="Email_Address_Status_Icon_2"&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for="Email_Address_3"&gt;Friend #3&lt;/label&gt;&lt;/div&gt;
    &lt;input type="text" id="Email_Address_3"&gt;
        &lt;span id="Email_Address_Status_Icon_3"&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

[{'answer_id': 69148, 'author': 'Pandincus', 'author_id': 2273, 'author_profile': 'https://Stackoverflow.com/users/2273', 'pm_score': 3, 'selected': True, 'text': '<p>Instead of using IDs for your email fields, you can give them each a class:</p>\n\n<pre><code>&lt;div&gt;\n    &lt;label for="Email_Address_1"&gt;Friend #1&lt;/label&gt;&lt;/div&gt;\n    &lt;input type="text" class="email"&gt;\n    &lt;span&gt;&lt;/span&gt;\n&lt;/div&gt;\n&lt;div&gt;\n    &lt;label for="Email_Address_2"&gt;Friend #2&lt;/label&gt;&lt;/div&gt;\n    &lt;input type="text" class="email"&gt;\n    &lt;span&gt;&lt;/span&gt;\n&lt;/div&gt;\n&lt;div&gt;\n    &lt;label for="Email_Address_3"&gt;Friend #3&lt;/label&gt;&lt;/div&gt;\n    &lt;input type="text" class="email"&gt;\n    &lt;span&gt;&lt;/span&gt;\n&lt;/div&gt;\n</code></pre>\n\n<p>Then, instead of selecting $("#Email_Address_Status_Icon_1"), you can select $("input.email"), which would give you a jQuery wrapped set of all input elements of class email.</p>\n\n<p>Finally, instead of referring to the status icon explicitly with an id, you could simply say:</p>\n\n<pre><code>$(this).next("span").removeClass(\'error\').html(\'Validating Email\');\n</code></pre>\n\n<p>\'this\' would be the email field, so \'this.next()\' would give you its next sibling. We apply the "span" selector on top of that just to be sure we\'re getting what we intend to. $(this).next() would work the same way.</p>\n\n<p>This way, you are referring to the status icon in a relative manner.</p>\n\n<p>Hope this helps!</p>\n'}, {'answer_id': 69241, 'author': 'Brian Boatright', 'author_id': 3747, 'author_profile': 'https://Stackoverflow.com/users/3747', 'pm_score': 0, 'selected': False, 'text': '<p>Thanks! Here is the completed refactor with your suggested changes.</p>\n\n<pre><code>&lt;script language="javascript"&gt;\n        $(document).ready(function() {\n            $(\'#Email_Address_1\').keyup(function(){Update_Email_Validate_Status(this)});\n            $(\'#Email_Address_2\').keyup(function() { Update_Email_Validate_Status(this)});\n            $(\'#Email_Address_3\').keyup(function() { Update_Email_Validate_Status(this)});            \n        });\n\n        function Update_Email_Validate_Status(field) {\n            var t = field;\n            if (t.value != t.lastValue) {\n                if (t.timer) clearTimeout(t.timer);\n                $(t).next("span").removeClass(\'error\').html(\'Validating Email\');\n\n                t.timer = setTimeout(function() {\n                    if (IsEmail(t.value)) {\n                        $(t).next("span").removeClass(\'error\').html(\'Valid Email\');\n                    } else {\n                    $(t).next("span").removeClass(\'error\').html(\'Not a valid Email\');\n                    };\n                }, 200);\n\n                t.lastValue = t.value;\n            }\n        }\n\n        function IsEmail(email) {\n            var regex = /^([a-zA-Z0-9_\\.\\-\\+])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$/;\n            if (regex.test(email)) return true;\n            else return false;\n        }  \n    &lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 69299, 'author': 'Geoff', 'author_id': 10427, 'author_profile': 'https://Stackoverflow.com/users/10427', 'pm_score': 0, 'selected': False, 'text': '<p>I would do :</p>\n\n<pre><code>$(document).ready(function() {\n        $(\'.validateEmail\').keyup(function(){Update_Email_Validate_Status(this)});            \n    });\n</code></pre>\n\n<p>Then add class=\'validateEmail\' to all your email inputs.</p>\n\n<p>Alternatively look into <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" rel="nofollow noreferrer">Form Validation Plugin</a>  i have used this a lot and it is very flexible and nice to use.  Saves you re-inventing...</p>\n'}]
['https://Stackoverflow.com/questions/69107', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3747/']