...

View Full Version : How to make a user to select an option from select drop down list



jennypretty
05-10-2007, 06:46 PM
Hello,
How do I require the user to select at least 1 option from the drop down list?
Here is my select?
I tried to use this:
if (document.frmName.a1.value=="") {
msgbox = msgbox + "\n >>> Please select one";

But it does not work.
Can you please help?

thanks.

My code:

<select name="select" size="1">
<option>Please select one</option><option>a</option><option>b</option>
</select>

Philip M
05-10-2007, 08:32 PM
if (document.frmName.select.selectedIndex.value==0) { // i.e the first (or zeroth) element in the options[] array

As always it is not a good idea to name select name = "select", form name = "form", text name = "text" and so on, as confusion can easily set in.

jennypretty
05-11-2007, 02:00 PM
I tried as you said but nothing happenned.
I tried to use a function chFrm to check the empty fields of FirstName and it worked fine, but when I use the same rule for select option box, nothing happen.

function chFrm() {
var msgbox = "You forgot the following question (s)\n";
var goon = msgbox
if (document.frmName.select.selectedIndex.value==0) {
msgbox = msgbox + "\n Please select one";
}
if (document.frmName.firstname.value=="") {
msgbox = msgbox + "\n >>> Enter first name";
}

Philip M
05-11-2007, 06:19 PM
Change
if (document.frmName.select.selectedIndex.value==0) {
to
if (document.frmName.select.selectedIndex==0) {

jennypretty
05-11-2007, 06:38 PM
It worked 50%. When I unselect any item from the drop boxes and hit submit button, it pops up an error message, but it does the same thing when I select one value from drop box.

Any idea why?

thanks.

glenngv
05-11-2007, 08:36 PM
Post the updated script so that we can see what's wrong.

jennypretty
05-11-2007, 08:55 PM
Here is my code.
Thanks.

<html>
<head>
<title></title>
<script language="javascript">
<!--
function chFrm() {
var msgbox = "You forgot the following question (s)\n";
var goon = msgbox

if (document.frmName.select.selectedIndex.value==0) {
msgbox = msgbox + "\n Please select one";
}
if (document.frmName.firstname.value=="") {
msgbox = msgbox + "\n >>> Enter first name";
}

// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {

alert(msgbox);
return false;
}
}
// End -->
</script>
</head>

<body>

<form name="frmName">
<select name="Rank" size="1">
<option>Please select one</option>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
</form>

</body>
</html>

glenngv
05-11-2007, 09:04 PM
<html>
<head>
<title></title>
<script language="javascript">
<!--
function chFrm(frm) {
var msgbox = "You forgot the following question (s)\n";
var goon = msgbox;

if (frm.Rank.selectedIndex==0) {
msgbox += "\n Please select one";
}
if (frm.firstname.value=="") {
msgbox += "\n >>> Enter first name";
}

// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {
alert(msgbox);
return false;
}
}
// End -->
</script>
</head>

<body>

<form name="frmName" onsubmit="return chFrm(this);">
<select name="Rank" size="1">
<option>Please select one</option>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
<input type="text" name="firstname" />

<input type="submit" value="Submit" />
</form>

</body>
</html>

jennypretty
05-14-2007, 01:59 PM
It worked. Woh!
Thanks.

jennypretty
05-31-2007, 08:32 PM
Hello,

I tried to add one more selection and the 2nd selection box does not validate. Is this code working for ONE the 1st selection box ONLY?

Please help.
Thanks.

Here is my code:
<html>
<head>
<title></title>
<script language="javascript">
<!--
function chFrm(frm) {
var msgbox = "You forgot the following question (s)\n";
var goon = msgbox;

if (frm.Rank.selectedIndex==0) {
msgbox += "\n Please select one";
}
if (frm.firstname.value=="") {
msgbox += "\n >>> Enter first name";
}
if (frm.Married.selectedIndex==0) {
msgbox += "\n Please select one";
}

// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {
alert(msgbox);
return false;
}
}
// End -->
</script>
</head>

<body>

<form name="frmName" onsubmit="return chFrm(this);">
<select name="Rank" size="1">
<option>Please select one</option>
<option>a</option><option>b</option><option>c</option>
</select>
<input type="text" name="firstname" />
<select name="Married" size="1">
<option>Please select one</option>
<option>Yes</option><option>No</option>

</select>
<input type="submit" value="Submit" />
</form>

</body>
</html>

glenngv
05-31-2007, 08:53 PM
I copied and pasted your code and ran it and it's working fine for me. It validates both dropdowns.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum