List box Validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adam.lock@gmail.com

    List box Validation

    Hi

    I have a form which has 10 list boxes on it, each with a select option
    with values 0 to 3.
    Once the user submits the form, I want to validate that only three of
    the list boxes have been selected and the total value is equal to 6,
    else they get an alert message.
    The validation is performed on an asp page.

    Would appreciate some help as I am lost with js!!

    Cheers
    Adam

  • Danny

    #2
    Re: List box Validation


    Define 'selected', all Select elements do have a .selected option, so, if
    you have 10 selects, you have at least 10 selected options, 1 per each, if
    you never defined one in the code, the 1st one is used, 1 per each
    provided you're having MULTIPLE on any of them. It'd help if you pasted
    the relevant code.

    Danny

    On Fri, 24 Jun 2005 14:18:26 -0700, <adam.lock@gmai l.com> wrote:
    [color=blue]
    > Hi
    >
    > I have a form which has 10 list boxes on it, each with a select option
    > with values 0 to 3.
    > Once the user submits the form, I want to validate that only three of
    > the list boxes have been selected and the total value is equal to 6,
    > else they get an alert message.
    > The validation is performed on an asp page.
    >
    > Would appreciate some help as I am lost with js!!
    >
    > Cheers
    > Adam
    >[/color]



    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

    Comment

    • Lee

      #3
      Re: List box Validation

      adam.lock@gmail .com said:[color=blue]
      >
      >Hi
      >
      >I have a form which has 10 list boxes on it, each with a select option
      >with values 0 to 3.
      >Once the user submits the form, I want to validate that only three of
      >the list boxes have been selected and the total value is equal to 6,
      >else they get an alert message.
      >The validation is performed on an asp page.
      >
      >Would appreciate some help as I am lost with js!![/color]

      You should be more clear about your requirements.
      Do you mean that *exactly* three of the Selection boxes should
      have non-zero values, or that it should be no more than three?
      Are they all "select-one"?
      Are there any other Selection boxes in the same form?

      Here's a solution based on my guesses:

      <html>
      <head>
      <script type="text/javascript">
      function validate(f) {
      var selectCount=0;
      var total=0;
      for (var i=0;i<f.element s.length;i++) {
      if ("select-one"==f.element s[i].type) {
      if (f.elements[i].value > "0") {
      selectCount++;
      total+=+f.eleme nts[i].value;
      }
      }
      }
      if (selectCount != 3 || total != 6 ) {
      alert("Select 3, with a total of 6.\n" +
      "You selected "+selectCou nt+
      ", with a total of "+total);
      return false;
      }
      }
      </script>
      </head>
      <body>
      <form onsubmit="retur n validate(this)" >
      <select>
      <option value="0">zero</option>
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three </option>
      </select><br>
      <select>
      <option value="0">zero</option>
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three </option>
      </select><br>
      <select>
      <option value="0">zero</option>
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three </option>
      </select><br>
      <select>
      <option value="0">zero</option>
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three </option>
      </select><br>
      <input type="submit">
      </form>
      </body>
      </html>

      Comment

      • adam.lock@gmail.com

        #4
        Re: List box Validation

        Hi

        Thanks for the replies, and sorry about the ambiguity!
        Code below, have removed the asp code from the top of the page. The
        page posts to itself, updates and then redirects to the next page.

        I need client side validation so a user only select 3 of the list boxes
        and the combined total is equal to six, otherwise they get an alert
        message.

        Many Thanks
        Adam:

        <body bgcolor="#FFFFF F" text="#000000" leftmargin="0" topmargin="0"
        marginwidth="0" marginheight="0 " link="#FFFFFF" vlink="#CCCCCC"
        alink="#FFFFFF" >
        <table width="100%" border="0" cellspacing="0" cellpadding="0"
        height="100%">
        <tr>
        <td height="109" background="bg. jpg"><img src="logo.jpg"
        width="830" height="109"></td>
        </tr>
        <tr align="center" valign="top">
        <td>
        <form ACTION="questio n30.asp?sumit=1 " METHOD="POST" name="form1"[color=blue]
        > <table width="50%" border="0" cellspacing="0" cellpadding="4" >[/color]
        <tr>
        <td colspan="2">Q30 .


        Please rank which of the following motivates you at work the most:<br>
        Please rank the top 3 only - 1 being most important, 2<br>
        being second most important and 3 being the third most
        important.<br></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td align="center"> &nbsp;</td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td align="center"> &nbsp;</td>
        </tr>
        <tr>
        <td> Money </td>
        <td align="center"> <select name="Q30Money" id="30Money "
        onChange="javas cript:limitOpti ons(this,3)">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td> Promotional prospects </td>
        <td align="center"> <select name="Q30Promot ionalprospects"
        id="30Promotion al prospects"
        onChange="javas cript:limitOpti ons(this,3)">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Job title/seniority</td>
        <td align="center"> <select name="Q30Jobtit le"
        id="30Jobtitle " onChange="javas cript:limitOpti ons(this,3)">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Increased responsibility</td>
        <td align="center"> <select
        name="Q30Increa sedresponsibili ty" id="30Increase d responsibility"
        onChange="javas cript:limitOpti ons(this,3)">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Creativit y of projects</td>
        <td align="center"> <select name="Q30Creati vityofprojects"
        id="30Creativit y of projects">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Personal development</td>
        <td align="center"> <select name="Q30Person aldevelopment"
        id="30Personal development">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Recogniti on from your peers</td>
        <td align="center"> <select
        name="Q30Recogn itionfromyourpe ers" id="30Recogniti on from your peers">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Recogniti on from your boss</td>
        <td align="center"> <select
        name="Q30Recogn itionfromyourbo ss" id="30Recogniti on from your boss">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>Recogniti on from your clients</td>
        <td align="center"> <select
        name="Q30Recogn itionfromyourcl ients" id="30Recogniti on from your
        clients">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td> Training </td>
        <td align="center"> <select name="Q30Traini ng"
        id="30Training ">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td> Working environment </td>
        <td align="center"> <select name="Q30Workin genvironment"
        id="30Working environment">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td> Variety of projects </td>
        <td align="center"> <select name="Q30Variet yofprojects"
        id="30Variety of projects">
        <option value="0" selected>Please Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td align="center"> &nbsp;</td>
        </tr>
        <tr>
        <td colspan="2"><in put type="submit" name="Submit"
        value="Next"></td>
        </tr>
        </table>
        <input type="hidden" name="submit" value="1">
        </form>
        </td>
        </tr>
        </table>
        </body>
        </html>

        Comment

        • adam.lock@gmail.com

          #5
          Re: List box Validation

          Lee thanks for the script.

          Works exactly as I needed.

          Really appreciate your time and help on this.

          Cheers
          Adam

          Comment

          Working...