javascript innerhtml to print for loop : I have just started HTML and JavaScript and got stuck here.I am trying to validate form in case of empty field and print the corresponding output using for loop .Below is the jsp page and javascript code.

<!-- begin snippet: js hide: false -->

<!-- language: lang-js -->

    < script >
      var err = new Array();
    var i = 0,
      flag = 0;

    function validateForm() {
      var x = document.forms["RegistrationController"]["firstname"].value;
      if (x == null || x == "") {
        err[i++] = "First name cannot be empty";
        flag = 1;
      }
      var a = document.forms["RegistrationController"]["dob"].value;
      if (a == null || a == "") {
        err[i++] = "Date of birth cannot be empty";
        flag = 1;
      }
      var b = document.forms["RegistrationController"]["address_line_1"].value;
      if (b == null || b == "") {
        err[i++] = "Address cannot be empty";
        flag = 1;
      }
      var c = document.forms["RegistrationController"]["city"].value;
      if (c == null || c == "") {
        err[i++] = "city cannot be empty";
        flag = 1;
      }
      var d = document.forms["RegistrationController"]["pincode"].value;
      if (d == null || d == "") {
        err[i++] = "pincode cannot be empty";
        flag = 1;
      }
      var e = document.forms["RegistrationController"]["mobile_no"].value;
      if (e == null || e == "") {
        err[i++] = "mobile no cannot be empty";

        flag = 1;
      }
      var f = document.forms["RegistrationController"]["email"].value;
      if (f == null || f == "") {
        err[i++] = "email cannot be empty";
        flag = 1;
      }
      if (flag == 1) {
        var string = "";
        for (j = 0; j < err.length; j++) {
          document.getElementById("error").innerHTML = err[i];
          return false;
        }
      }

    } < /script>

<!-- language: lang-html -->

    <html>

    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

      <title>Registration</title>
    </head>

    <body>
      <div class="error" id="error"></div>
      <h1>Registration Form</h1>
      <div class="ex">
        <form name="RegistrationController" method="post" onsubmit="return validateForm()">
          <table style="with: 50%">
            <tr>
              <td>First name</td>
              <td>
                <input type="text" name="firstname" />
              </td>
            </tr>

            <tr>
              <td>Last name</td>
              <td>
                <input type="text" name="lastname" />
              </td>
            </tr>
            <tr>
              <td>Date of Birth</td>
              <td>
                <input type="text" name="dob" />
              </td>
            </tr>
            <tr>
              <td>Address Line 1</td>
              <td>
                <input type="text" name="address_line_1" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2</td>
              <td>
                <input type="text" name="address_line_2" />
              </td>
            </tr>
            <tr>
              <td>state</td>
              <td>
                <input type="text" name="state" />
              </td>
            </tr>
            <tr>
              <td>city</td>
              <td>
                <input type="text" name="city" />
              </td>
            </tr>
            <tr>
              <td>pincode</td>
              <td>
                <input type="text" name="pincode" />
              </td>
            </tr>
            <tr>
              <td>Mobile no</td>
              <td>
                <input type="text" name="mobile_no" />
              </td>
            </tr>
            <tr>
              <td>email</td>
              <td>
                <input type="text" name="email" />
              </td>
            </tr>
            <tr>
              <td>gender</td>
              <td>
                <input type="radio" name="gender" />Male</td>
              <td>
                <input type="radio" name="gender" />Female</td>
            </tr>
          </table>
          <input type="submit" value="register" />
        </form>
      </div>

    </body>

    </html>

<!-- end snippet -->

