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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select box and alert

Status
Not open for further replies.

newbby

Programmer
Mar 16, 2004
36
US
Hi all,
I have a select box:
<form name="frmname">
<select name="strselect">
<option value="yes">yes
<option value="no">no
</select>
<input type="text" name="stranswer">
</form>

I need to write a javascript to check that:
if users select "yes" in 'strselect' then, they have to provide an answer in "stranswer". How can I force the form to not submit with alert/message box and focus().

Thanks
 
Hi all,
where do I include the event handler like onclick or onsubmit?
thanks
 
typically you would write a function to call from the form's onsubmit handler:
Code:
<script type="text/javascript">
function validate(f) {
  if (f.strselect.options[f.selectedIndex].value == "yes") {
    if (!f.stranswer.value) {
      alert("Please enter an answer");
      f.stranswer.focus();
      return false;
    }
  }
  return true;
}
</script>

<form name="frmname" onsubmit="return validate(this);">
<select name="strselect">
<option value="yes">yes
<option value="no">no
</select>
<input type="text" name="stranswer">
</form>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top