Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Menu validation

Status
Not open for further replies.

MikeDee

Programmer
Aug 2, 2001
19
GB
If I had a list box like (below).
Is it possible to have a submit button to detect if someone has selected an option in the menu?

So if someone just press the submit without selecting an option in the menu a Javascript will alert("error");


<select name=&quot;bactivity&quot; size=&quot;1&quot; length=&quot;100&quot;&quot;>
<option value=&quot;Selection&quot; selected>Select one option</option> <option>Agriculture/Forestry/Fishing</option>
<option>Business Services</option>
<option>Central/Local Government</option>
</select>
 
Have a look at this, it maydo what you want:

Code:
<HTML>
<!-- CREATION DATE: 08/08/01 -->
<HEAD>
<TITLE></TITLE>

function checkIt(form)
{
   if (form.bactivity.value == '0')
      alert ('Not Selected');
}
</SCRIPT>
</HEAD>
<BODY>
<form>
<select name=&quot;bactivity&quot; size=&quot;1&quot; length=&quot;100&quot;&quot;>
<option value=&quot;0&quot; selected>Select one option</option> 
<option Value=&quot;1&quot;>Agriculture/Forestry/Fishing</option>
<option Value=&quot;2&quot;>Business Services</option>
<option Value=&quot;3&quot;>Central/Local Government</option>
</select>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Press Me&quot; onClick=&quot;checkIt(this.form)&quot;>
</form>
</BODY>
</HTML>

X-) Billy H

bhogar@acxiom.co.uk
 
Slight modification to WildWill's code:

<HTML>
<!-- CREATION DATE: 08/08/01 -->
<HEAD>
<TITLE></TITLE>

function checkIt(form)
{
if (form.bactivity.value == '0') {
alert ('Not Selected');
return false;]{
}
else return true;

}
</SCRIPT>
</HEAD>
<BODY>
<form>
<select name=&quot;bactivity&quot; size=&quot;1&quot; length=&quot;100&quot;&quot;>
<option value=&quot;0&quot; selected>Select one option</option>
<option Value=&quot;1&quot;>Agriculture/Forestry/Fishing</option>
<option Value=&quot;2&quot;>Business Services</option>
<option Value=&quot;3&quot;>Central/Local Government</option>
</select>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Press Me&quot; onClick=&quot;return checkIt(this.form)&quot;>
</form>
</BODY>
</HTML>


Mise Le Meas,

Mighty :)
 
no, guyz, must be smth like this:

if (form.bactivity[form.bactivity.selectedIndex].value == '0')
if u want it 2 work in netscrap as well..

the rest of Mighty's code must be workin nice.. regards, vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top