Javascript : Issue validating a regular expression
<p>I have a JavaScript which validates a regular expression as below:</p>

<pre><code>&lt;script type="text/javascript"&gt;
var ck_git = /^(?!\d+$)[a-zA-Z0-9_,]*$/;
function validate(form){
    var git1 = form.git_repos_name_1.value;
    var git2 = form.git_repos_name_2.value;
    var git3 = form.git_repos_name_3.value;
    var errors = [];
    if (!ck_git.test(git1)) {
    errors[errors.length] = "Git Name 1 not valid";
    }
    if (!ck_git.test(git2)) {
    errors[errors.length] = "Git Name 2 not valid";
    }
    if (!ck_git.test(git3)) {
    errors[errors.length] = "Git Name 3 not valid";
    }
    if (errors.length &gt; 0) {
    reportErrors(errors);
    return false;
    }
    return true;
    }
    function reportErrors(errors){
    var msg = "Please Enter Valide Data:\n";
    for (var i = 0; i&lt;errors.length; i++) {
    var numError = i + 1;
    msg += "\n" + numError + ". " + errors[i];
    }
    alert(msg);
}
&lt;/script&gt;
</code></pre>

<p>I am using <code>onsubmit="return validate(this);"</code> in my form to validate this javascript. 
All I am trying to do in the above javascipt is taking text field id's and assigning to variables and validating. </p>

<p><strong>Question:</strong> Initially my form does not have any of those text fields which i am validating from the javascript. The text fields appears only when the user clicks on Add More button. I am doing this by Jquery function which generates the 3 values i.e <code>git_repos_name_1, git_repos_name_2, git_repos_name_3</code>. </p>

<p>The Problem I am facing now is that if the user has clicked the add more button once so now he has <code>git_repos_name_1</code> and has got only one text field to enter and does not click on the add more button again 2 more times thus <code>git_repos_name_2</code> &amp; <code>git_repos_name_3</code> are not present. Now the validation is not happening. But when the user clicks on add more thrice and adds everything the validation is happening. </p>

<p>I want validations to happen for each and every text box only if its added. If its not then we skip.   </p>

<p>Hope I have made myself clear. I am a Ruby On rails developer and finding it hard to proceed with this. </p>

<p>If you care how my <strong>jquery</strong> to add the 3 buttons looks, here it is:</p>

<pre><code>&lt;script type="text/javascript"&gt;
    $(document).ready(function(){
     var counter = 1;
     $("#addButton").click(function () {
    if(counter&gt;4){
            alert("Not allowed to add more then 3 repositories");
            return false;
    }   
    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);
        newTextBoxDiv.html("&lt;input type='text' name='form[git_repos_name_"+counter+"]' value='' id='form_git_repos_name_"+counter+"' &gt;");
        newTextBoxDiv.appendTo("#TextBoxesGroup");
        counter++;
     });
      $("#removeButton").click(function () {
    if(counter==1){
          alert("No more to remove");
          return false;
       }   
    counter--;
         $("#TextBoxDiv" + counter).remove();
     });
  });
&lt;/script&gt;
</code></pre>
