/    Sign up×
Community /Pin to ProfileBookmark

Dropdown Box Send null

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>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceFeb 23.2006 — If you always provide a dummy option as the first selection, and make that dummy option "select"ed by default, then the following test indicates no selection has yet been made:

if (obj.selectedIndex <= 0)
Copy linkTweet thisAlerts:
@swtransauthorFeb 23.2006 — This seems to work. Seems a little ugly though, lol.


<html>

<head>

<script type="text/javascript">

function formCheck(formobj){

// Enter name of mandatory fields

var fieldRequired = Array("Student");

// Enter field description to appear in the dialog box

var fieldDescription = Array("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;}



if (document.myform.ReportType.options[document.myform.ReportType.options.selectedIndex].value == "



")

{

alert("You must choose a Report Type from the dropdown box");

formobj['ReportType'].focus();

return false;

}



}

</script>



<FORM name="myform" id="sendit" ENCTYPE="multipart/form-data" ACTION="sent.htm" onsubmit="return



formCheck(this)" METHOD=POST>



Report Type:<br>

<select name="ReportType">

<option value= " " selected>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>
×

Success!

Help @swtrans spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...