On submit of this JSP, users should be required to select something other than "Please Select." If a user selects "Please Select" and submits the form, it should alert the user with a popup that they should change the field.
<td class="normalTD" nowrap class="alignLeft" width="16%">
<form:select class="requiredAttr" path="detail[${loop.index}].cd" cssClass="fotnStyle" id="cd" >
<form:option value="">Please Select</form:option>
<c:forEach var="list" items="${override.list}">
<c:choose>
<c:when test="${override.listne null}">
<c:choose>
<c:when test="${list.listCd eq detail.cd}">
<c:set var="selectStatus" value="SELECTED" />
</c:when>
<c:otherwise>
<c:set var="selectStatus" value="" />
</c:otherwise>
</c:choose>
</c:when>
</c:choose>
<option value="${list.listCd}" ${selectStatus}>
<c:out value="${list.desc}" />
</option>
</c:forEach>
</form:select>
</td>
I used the following jQuery snippet and it worked fine on alerting users that a textbox was not filled, but did not affect the dropdown.
function validate(){
$(".requiredAttr").each(function(){
if($(this).val().length < 1 || $(this).val() == ""){
alert("Please fill in all the required fields.");
$(this).focus();
return false;
}else{
return true;
}
});
return false;
}
This is the generated HTML.
<td class="normalTD" nowrap class="alignLeft" width="16%">
<select id="cd" name="detail[0].cd" class="fotnStyle" class="requiredAttr">
<option value="">Please Select</option>
<option value="1" >A</option>
<option value="2" >B</option>
<option value="3" >C</option>
<option value="4" >D</option>
</select>
</td>