require field only if box is checked

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darren

    #1

    require field only if box is checked

    How can I require a textbox only if a checkbox is checked.

    If the user checks the box for "fruit" then they have to enter the type of
    fruit

    If the user does not check the "fruit" box then they do not have to enter
    the type of fruit.

    Thanks!
    Darren


  • Lee

    #2
    Re: require field only if box is checked

    Darren said:[color=blue]
    >
    >How can I require a textbox only if a checkbox is checked.
    >
    >If the user checks the box for "fruit" then they have to enter the type of
    >fruit
    >
    >If the user does not check the "fruit" box then they do not have to enter
    >the type of fruit.[/color]

    If fruit is not checked, don't bother to check to see
    if they've entered the type of fruit.
    What part of that do you need help with?

    Comment

    • McKirahan

      #3
      Re: require field only if box is checked

      "Darren" <darren@lan-specialist.com> wrote in message
      news:d2yLc.1529 7$W86.1230@nwrd ny03.gnilink.ne t...[color=blue]
      > How can I require a textbox only if a checkbox is checked.
      >
      > If the user checks the box for "fruit" then they have to enter the type of
      > fruit
      >
      > If the user does not check the "fruit" box then they do not have to enter
      > the type of fruit.
      >
      > Thanks!
      > Darren[/color]

      Perhaps something like this; watch for word-wrap.

      <html>
      <head>
      <title>fruit.ht m</title>
      <script type="text/javascript">
      function check(what) {
      var form = document.forms[0];
      if (document.getEl ementById(what + "_box").checked ) {
      document.getEle mentById(what). disabled = false;
      } else {
      document.getEle mentById(what). disabled = true;
      document.getEle mentById(what). value = "";
      }
      }
      </script>
      </head>
      <body>
      <form>
      Fruit:
      <input type="checkbox" name="fruit_box " onclick="check( 'fruit')">
      <input type="text" name="fruit" disabled>
      </form>
      </body>
      </html>


      Comment

      • Darren

        #4
        Re: require field only if box is checked

        I need help with all of it.
        The form has a checkbox and a text field (this obviously a simplified
        version of my form)

        If they check the box then I need for the textfield to be required. If the
        box is not checked then the field is not required. The onlt validation that
        I have done is with Dreamweaver so I do not know Javascript validation.
        Thanks!


        "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
        news:cdmc9v0k8g @drn.newsguy.co m...[color=blue]
        > Darren said:[color=green]
        > >
        > >How can I require a textbox only if a checkbox is checked.
        > >
        > >If the user checks the box for "fruit" then they have to enter the type[/color][/color]
        of[color=blue][color=green]
        > >fruit
        > >
        > >If the user does not check the "fruit" box then they do not have to enter
        > >the type of fruit.[/color]
        >
        > If fruit is not checked, don't bother to check to see
        > if they've entered the type of fruit.
        > What part of that do you need help with?
        >[/color]


        Comment

        • Lee

          #5
          Re: require field only if box is checked

          Darren said:[color=blue]
          >
          >I need help with all of it.
          >The form has a checkbox and a text field (this obviously a simplified
          >version of my form)
          >
          >If they check the box then I need for the textfield to be required. If the
          >box is not checked then the field is not required. The onlt validation that
          >I have done is with Dreamweaver so I do not know Javascript validation.
          >Thanks![/color]

          Here's an example.
          The "if" condition is true if the "fruit" checkbox is checked
          and the value of the "fruitType" text field does NOT contain
          any non-blank characters.


          <html>
          <head>
          <script type="text/javascript">
          function validate(f) {
          if(f.fruit.chec ked && -1==f.fruitType. value.search(/\S/)) {
          alert("Please specify the type of fruit.");
          return false;
          }
          return true;
          }
          </script>
          </head>
          <body>
          <form onsubmit="retur n validate(this)" >
          <input type="checkbox" name="carrot">& nbsp;Carrot<br>
          <input type="checkbox" name="broccoli" >&nbsp;Broccoli <br>
          <input type="checkbox" name="fruit">&n bsp;Fruit&nbsp;
          <input name="fruitType "><br>
          <input type="submit" value="submit">
          </form>
          </body>
          </html>

          Comment

          Working...