...

View Full Version : Requiring a selection from a drop down box



Errica
09-08-2006, 04:06 PM
How can I force the user to choose one of the four?

<select name="product">
<option selected>Please choose...</option>
<option>product 01</option>
<option>product 02</option>
<option>product 03</option>
<option>product 04</option>
</select>

Thanks!

name _F1
09-08-2006, 04:13 PM
Drop-down boxes will always have at least on option selected, so I'm not quite sure what you mean?

Errica
09-08-2006, 04:15 PM
The "Please choose..." is selected by default. I want the user to be forced to choose one of the four below it.

arnyinc
09-08-2006, 04:34 PM
<html>
<head>
<script language="javascript">

function validate(fieldindex){
if (fieldindex==0){
alert('Please select a product');
return false;
}
return true;
}

</script>

</head>

<body>

<form onsubmit="return validate(this.product.selectedIndex)">

<select name="product">
<option selected>Please choose...</option>
<option>product 01</option>
<option>product 02</option>
<option>product 03</option>
<option>product 04</option>
</select>
<input type="submit">
</form>

</body>

</html>

Errica
09-08-2006, 05:10 PM
Works great although I failed to mention there are two other onsubmits and an onclick I'm using. :(

onSubmit="return (Require(this,'Name') && submitonce(this))"

onclick="return checkit()"

Here is the js for the them:


// Check for a valid email address

var testresults
function checkemail(){
var str=document.formCheck.Email.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Please input a valid email address.")
testresults=false
}
return (testresults)
}

function checkit(){
if (document.layers||document.getElementById||document.all)
return checkemail()
else
return true
}

// Check for required fields

function Require(obForm,szFields)
{
var fields = szFields.split(",")
var szMissing= new Array();
for (x=0;x<fields.length;x++) {
if (obForm.elements[fields[x]].value.length==0) {
szMissing[szMissing.length]=new String(fields[x]);
}
}
if (szMissing.length) {
alert("The"+((szMissing.length>1)?"s ":" ")+szMissing.join(",")+" field must be completed.");
return false
}
return true;
}

// Check for multiple submissions

function submitonce(theform){
//if IE 4+ or NS 6+
if (document.all||document.getElementById){
//screen thru every element in the form, and hunt down "submit" and "reset"
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
//disable em
tempobj.disabled=true
}
}
}

Any way to incorporate it?

Errica
09-11-2006, 07:36 PM
Adding this to the function "Require" worked:



function Require(obForm,szFields)
{
var fields = szFields.split(",")
var szMissing= new Array();
for (x=0;x<fields.length;x++) {
if (obForm.elements[fields[x]].value.length==0) {
szMissing[szMissing.length]=new String(fields[x]);
}
}
if (szMissing.length) {
alert("The"+((szMissing.length>1)?"s ":" ")+szMissing.join(",")+" field must be completed.");
return false
}
if (!document.formCheck.product.selectedIndex) {
alert("Please choose a product.");
return false
}
return true;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum