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

Checking select box before processing form 1

Status
Not open for further replies.

rmagan

Programmer
Jun 3, 2003
35
US
I have a form that uses a select box (multiple). When the users hits the 'Go' button, I need to make sure that the user selected at least 1 value from the select box. If they did, I need to process my form, otherwise display alert and cancel.

<html>
<head>
<html>
<head>
<title>Student Schedule</title>
<script type=&quot;text/javascript&quot;>
function fillFields(form){
var a = form.p_sched_value.value;
}
</script>
</head>
<form name=&quot;maint_student_schedule&quot; onSubmit=&quot;fillFields(this)&quot; action=&quot;stars3.star_portal.process_student_schedule&quot; method=&quot;post&quot;>
<table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=0 cellpadding=2 id=&quot;student_schedule&quot;>
<TBODY>
<table ALIGN=&quot;LEFT&quot;><br><tr>
<td ALIGN=&quot;RIGHT&quot;> <font CLASS=&quot;PortletText1&quot;>
<select name=&quot;p_sched_value&quot; id=&quot;sched_choice1&quot; SIZE=&quot;3&quot; MULTIPLE>
<option value=~~~~~~~~~~~~~01>01 &nbsp;&nbsp (07:59-08:44)
<option value=~~~~~~~~~~~~~02>02 &nbsp;&nbsp (08:45-09:25)
<option value=~~~~~~~~~~~~~03>03 &nbsp;&nbsp (09:25-10:10)
</select>
</td>
<td ALIGN=&quot;CENTER&quot;><input type=&quot;submit&quot; name=&quot;p_action&quot; value=&quot;Go&quot;>
</tr>
</table>
</tbody>
</form>
</table>
 
Hi,

Instead of <input type = &quot;submit&quot; ... use <input type=&quot;button&quot; and attach an onclick event that checks the selectedIndex of your dropdown list eg.

Code:
<td ALIGN=&quot;CENTER&quot;><input type=&quot;button&quot; name=&quot;p_action&quot; value=&quot;Go&quot; onCLick=&quot;return CheckList();&quot;>

the CheckList function is defined as follows ...
Code:
<script type=&quot;text/javascript&quot;>
  function CheckList(){

if(maint_student_schedule.sched_choice1.selectedIndex== -1){
  alert(&quot;Please select an option&quot;);
  return false;
  }
  else{
    maint_student_schedule.submit();
  }
}
</script>

Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top