Validate input if options combo selected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mariodavi
    New Member
    • Feb 2008
    • 8

    Validate input if options combo selected

    Hi everybody,

    how to validate input text in the follow:

    if option combo = 1,2 e 3, input 1 and input 2 must be filled, if one of this values not selected in combo (to be others), filled input 3.

    validate code with error below:

    [CODE=javascript]funcion validate () {
    ...
    valor = document.form.s elect.value;

    if (value === "1" || value === "2" || value === "3"){
    alert("Input 1 must be filled.");
    document.form.i nput1.focus();
    return false;
    } else if (input2 === "") {
    alert("Input 2 must be filled.");
    document.form.i nput2.focus();
    return false;
    } else {
    alert("Input 3 must be filled.");
    document.form.i nput3.focus();
    return false;
    }
    return true;
    }
    [/CODE]
    Thanks!
    Last edited by gits; Mar 6 '08, 09:08 AM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Originally posted by mariodavi
    if option combo = 1,2 e 3, input 1 and input 2 must be filled, if one of this values not selected in combo (to be others), filled input 3.
    Does this mean that if the combo box has options 1, 2, or 3 selected, you want to validate that inputs 1 and 2 are filled. If none of these are selected, then input 3 has to be filled. Is that correct?

    Comment

    • mariodavi
      New Member
      • Feb 2008
      • 8

      #3
      Originally posted by acoder
      Does this mean that if the combo box has options 1, 2, or 3 selected, you want to validate that inputs 1 and 2 are filled. If none of these are selected, then input 3 has to be filled. Is that correct?
      The combo box has 5 options, if one of this options (1,2,3) will selected then input text 1 and 2 must be filled (for the user). If selected option will selected 4 or 5 input text 3 must be filled. Ok?!

      Thanks!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        [CODE=javascript]function validate () {
        valor = document.form.s elect.value;

        if (valor == "1" || valor == "2" || valor == "3") {
        if (input1 == "") {
        alert("Input 1 must be filled.");
        document.form.i nput1.focus();
        return false;
        } else if (input2 == "") {
        alert("Input 2 must be filled.");
        document.form.i nput2.focus();
        return false;
        }
        } else {
        if (input3 == "") {
        alert("Input 3 must be filled.");
        document.form.i nput3.focus();
        return false;
        }
        }
        return true;
        }
        [/CODE]

        Comment

        • mariodavi
          New Member
          • Feb 2008
          • 8

          #5
          Dont works, cause always loop to first condition - Input 1 must be fille. =/

          Originally posted by acoder
          [CODE=javascript]function validate () {
          valor = document.form.s elect.value;

          if (valor == "1" || valor == "2" || valor == "3") {
          if (input1 == "") {
          alert("Input 1 must be filled.");
          document.form.i nput1.focus();
          return false;
          } else if (input2 == "") {
          alert("Input 2 must be filled.");
          document.form.i nput2.focus();
          return false;
          }
          } else {
          if (input3 == "") {
          alert("Input 3 must be filled.");
          document.form.i nput3.focus();
          return false;
          }
          }
          return true;
          }
          [/CODE]

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by mariodavi
            Dont works, cause always loop to first condition - Input 1 must be fille. =/
            Have you not set input1, input2, etc. to document.form.i nput1.value, etc.?

            Post the rest of the code especially how the function is called.

            Comment

            • mariodavi
              New Member
              • Feb 2008
              • 8

              #7
              And now, nothing works in the page =/

              [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
              <title>Untitl ed Document</title>
              <script type="text/javascript">
              function select_validate (formulario,cam po,rotulo){
              if(document[formulario][campo].selectedIndex == "0" ){
              document[formulario][campo].focus();
              alert('Especifi que '+rotulo+'!');
              return false;
              }
              return true;
              }
              </script>
              <script type="text/javascript">

              function validate () {
              // Option must be selected
              if (!select_valida te('form','comb oselect','Optio n')) {
              return false;
              }
              valor = document.form.c omboselect.valu e;
              if (valor === "1" || valor === "2" || valor === "3") {
              if (input1 == "") {
              alert("Input 1 must be filled.");
              document.form.i nput1.focus();
              return false;
              } else if (input2 == "") {
              alert("Input 2 must be filled.");
              document.form.i nput2.focus();
              return false;
              } else {
              if (input3 == "") {
              alert("Input 3 must be filled.");
              document.form.i nput3.focus();
              return false;
              }
              }
              return true;
              }
              </script>
              </head>

              <body>
              <form action="test_va lidate.html" method="post" name="form" onsubmit="retur n validate()">

              <select name="combosele ct">
              <option value="0" selected>-------------</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
              </select><br />

              <label>Input 1</label><input name="input1" size="40" type="text" /><br />
              <label>Input 2</label><input name="input2" size="40" type="text" /><br />
              <label>Input 3</label><input name="input3" size="40" type="text" /><br />
              <input type="submit" value="Send" title="Send" />
              </form>
              </body>
              </html>
              [/HTML]
              Originally posted by acoder
              Have you not set input1, input2, etc. to document.form.i nput1.value, etc.?

              Post the rest of the code especially how the function is called.
              Last edited by acoder; Mar 7 '08, 08:35 PM. Reason: Added code tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Please enclose your posted code in [code] tags (See How to Ask a Question).

                This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                Please use [code] tags in future. Thanks!

                Moderator.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by mariodavi
                  And now, nothing works in the page =/
                  That's because you've gone back to your old code! If you check the error console, you will see that you have a missing bracket. The inputs also need to be declared:
                  [code=javascript]function validate () {
                  // Option must be selected
                  if (!select_valida te('form','comb oselect','Optio n')) {
                  return false;
                  }
                  valor = document.form.c omboselect.valu e;
                  input1 = document.form.i nput1.value;
                  input2 = document.form.i nput2.value;
                  input3 = document.form.i nput3.value;
                  if (valor === "1" || valor === "2" || valor === "3") {
                  if (input1 == "") {
                  alert("Input 1 must be filled.");
                  document.form.i nput1.focus();
                  return false;
                  } else if (input2 == "") {
                  alert("Input 2 must be filled.");
                  document.form.i nput2.focus();
                  return false;
                  }
                  } else {
                  if (input3 == "") {
                  alert("Input 3 must be filled.");
                  document.form.i nput3.focus();
                  return false;
                  }
                  }
                  return true;
                  }[/code]

                  Comment

                  • mariodavi
                    New Member
                    • Feb 2008
                    • 8

                    #10
                    Thanks acoder =]

                    Sorry for the code unformated!

                    Originally posted by acoder
                    That's because you've gone back to your old code! If you check the error console, you will see that you have a missing bracket. The inputs also need to be declared:
                    [code=javascript]function validate () {
                    // Option must be selected
                    if (!select_valida te('form','comb oselect','Optio n')) {
                    return false;
                    }
                    valor = document.form.c omboselect.valu e;
                    input1 = document.form.i nput1.value;
                    input2 = document.form.i nput2.value;
                    input3 = document.form.i nput3.value;
                    if (valor === "1" || valor === "2" || valor === "3") {
                    if (input1 == "") {
                    alert("Input 1 must be filled.");
                    document.form.i nput1.focus();
                    return false;
                    } else if (input2 == "") {
                    alert("Input 2 must be filled.");
                    document.form.i nput2.focus();
                    return false;
                    }
                    } else {
                    if (input3 == "") {
                    alert("Input 3 must be filled.");
                    document.form.i nput3.focus();
                    return false;
                    }
                    }
                    return true;
                    }[/code]

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      No problem. Glad to hear that it now works.

                      Comment

                      Working...