title: Javascript validation for dynamically generated forms in Django template
body: <p>I am struggling with something that is probably very basic: I need to generate a form with marks for my University database application. Each student in each module has a class got "Performance" that stores all the marks for the module. There are different assessments and the Performance class calculates the average for them.</p>
  
  <p>I need to be able to enter, for example, all the marks for the first assessment, and I did that with a dynamically generated Django Form as a table in the template:</p>
  
  <pre><code>{% for performance in performances %}
      &lt;tr&gt;
          &lt;td&gt;
              {{ performance.student }}
          &lt;/td&gt;
          &lt;td&gt;
              {% if to_mark == 1 %} 
                  &lt;input type="text" class="input-mini" name="{{ student.student_id }}" value="{{ performance.assessment_1 }}"&gt;
              {% else %}
                  {{ performance.assessment_1 }}
              {% endif %}
          &lt;/td&gt;
  
         And the same for the other assessments (to_mark gets passed on by views.py to indicate which assessments needs to be marked here)
  </code></pre>
  
  <p>I have failed to use Inlineformfields and therefore decided to generate a form dynamically and validate it with Javascript, also because the validation is simple (has to be a number between 1 and 100), and also because I want to use Javascript to calculate the average on the fly.</p>
  
  <p>My problem is that I have no clue about Javascript. All the tutorials (like this one <a href="http://www.w3schools.com/js/js_form_validation.asp" rel="nofollow">http://www.w3schools.com/js/js_form_validation.asp</a>) use the name of the form field in the Javascript function, but in my case that name is dynamically generated, so that I can access it easily in the views.py:</p>
  
  <pre><code>if student.student_id in request.POST:
      tmp = request.POST[student.student_id]
      try:
          mark = int(tmp)
          if mark in range(0, 100):
          performance = Performance.objects.get(student=student, module=module)
          if to_change == 1:
              performance.assessment_1 = mark
         ...and so on for the other assessments
      except ValueError:
          pass (in case someone accidentally enters a letter and not a number)
  </code></pre>
  
  <p>Is there a way I can use Javascript to address my form fields? Or should I use a different approach than taking the student_id as the name to read it out? How could I do that?</p>
  
  <p>Thanks,
  Tobi</p>