I have following requirements:
- Get questions from db in servlet.
- Display themin jsp.
- Check if question is mandatory or not.
- If question is mandatory then answer to that is reuired at the time of submiting form and if no answer is provided for the question then don't submit form and display warning.
Up to number 3 I have done it. But don't know how to go about number 4. How can I check this in javascript. As number of questions are not fixed as they are coming from database. Also how many of them are mandatory is also not fixed. How to go for this?
What value should I check in javascript? How can I know that which questions are mandatory in javascript once they are displayed in page?
---- Edit
This is how my data is displayed on jsp:
for (Question question : questions) {%>
<tr>
<td><b><%= question.getDefination() %></b>
<table style="margin-left: 25px;">
<%
int type = question.getType();
List<Option> options = question.getOptions();
for(Option option: options){%>
<tr>
<% if(type == 2){ %>
<td><input type="radio" name="<%=question.getIdQuestion() %>" value="<%= option.getIdQueOption() %>" /><%= option.getOptionName() %></td>
<%} else if(type == 1) { %>
<td><input type="checkbox" name="<%=option.getIdQueOption() %>" value="<%= option.getIdQueOption() %>" /><%= option.getOptionName() %></td>
<%} %>
</tr>
<%}%>
</table></td></tr>
<%}%>
--- Edit 2 Sample page will look like this:

class="mandatory", then you can add a validation function (called from form's onsubmit event) that loops through all the elements with that class. I wouldn't reinvent the wheel though: there are several libraries that have customisable validation code (there are several jQuery validation plugins, for example).