/    Sign up×
Community /Pin to ProfileBookmark

Requiring a selection from a drop down box

The “Please choose…” is selected by default. I want the user to be forced to choose one of the four below it.

<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>

The caveat is that I’m already using two onsubmits and an onclick:

onSubmit=”return (Require(this,’Name’) && submitonce(this))”

onclick=”return checkit()”

Here is the js for the them:

[code]
// 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
}
}[/code]

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemSep 11.2006 — The "Please choose..." is selected by default. I want the user to be forced to choose one of the four below it.

<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>

[/QUOTE]
Add to your [I]Require[/I] function:[CODE]if(!document.formCheck.product.selectedIndex)
{alert.......}[/CODE]
Copy linkTweet thisAlerts:
@AshleyQuickauthorSep 11.2006 — Thanks!
×

Success!

Help @AshleyQuick 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.18,
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,
)...