I made this example to simplify a problem I’m having. This script checks to make sure form elements have a value. It is working on the text field but not the dropdown box. I think I’m sending the null from the dropdown box, but this seems to only check one field.
<head>
<script type=”text/javascript”>
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array(“ReportType”,”Student”);
// Enter field description to appear in the dialog box
var fieldDescription = Array(“ReportType”,”Student”);
// dialog message
var alertMsg = “Please complete the following fields:n”;
var l_Msg = alertMsg.length;
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case “select-one”:
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == “”){
alertMsg += ” – ” + fieldDescription[i] + “n”;}
break;
case “select-multiple”:
if (obj.selectedIndex == -1){
alertMsg += ” – ” + fieldDescription[i] + “n”;}
break;
case “text”:
case “textarea”:
if (obj.value == “” || obj.value == null){
alertMsg += ” – ” + fieldDescription[i] + “n”;}
break;
default:}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;}}
if (!blnchecked){
alertMsg += ” – ” + fieldDescription[i] + “n”;}}}}
if (alertMsg.length == l_Msg){
//return true;
}else{
alert(alertMsg);
return false;}
}
</script>
<FORM id=”sendit” ENCTYPE=”multipart/form-data” ACTION=”sent.htm” onsubmit=”return formCheck(this)” METHOD=POST>
Report Type:<br>
<select name=”ReportType”>
<option selected value = null>Select Type</option>
<option value=”Classroom”>Classroom</option>
<option value=”Student”>Student</option>
</select>
<br><br>
Student’s Name<br>
<input type=”text” name=”Student” size=”20″ maxlength=”100″>
<br><br>
<input type=”submit” value=”Send”>
</td>
</tr>
</form>