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

validate that all select boxes have a selcted value before submitting

Status
Not open for further replies.

ctenicki

Programmer
May 13, 2002
10
US
I'm trying to run a validator script from a submit that would check to make sure that all select boxes have a chosen value in them. The problem im having is that I cant just do a ..........
if (form.reason.value == "item") {
alert("You must select a reason");
form.reason.focus();
return false; }

where "reason" is the name of the select box. this page is built dynamically and the names of the select boxes are generated from XSL. So there aren't a set number of boxes or names that I can use to run this script on. i basically need to run a script(checking all select boxes no matter how many there are what name they might have) to make sure that they all have a value selected. Thanks for your help.
 
Try this...
Code:
<html>
  <head>
    <script language=javascript>
      function chkMe() {
        var j = 0;
        for (var i = 0; i < document.forms[0].elements.length; i++) {
          if (document.forms[0].elements[i].type == 'select-one')
            j = j + 1;
            wSelect = eval(&quot;document.forms[0].&quot; + document.forms[0].elements[i].name);
            if (wSelect.options[wSelect.selectedIndex].value == 'blank') {
              alert(&quot;Please select an option.&quot;);
              wSelect.focus();
              return false;
            }
         }
       }
    </script>
  </head>
<form>
<select name=wl_1>
  <option value=&quot;blank&quot;>Choose One...</option>
  <option value=&quot;1&quot;>1</option>
  <option value=&quot;2&quot;>2</option>
  <option value=&quot;3&quot;>3</option>
</select>
<br>
<select name=wl_2>
  <option value=&quot;blank&quot;>Choose One...</option>
  <option value=&quot;a&quot;>a</option>
  <option value=&quot;b&quot;>b</option>
  <option value=&quot;c&quot;>c</option>
</select>
<br>
<input type=button name=wb_test value=&quot;Validate&quot; onClick=&quot;chkMe()&quot;>
</form>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top