Make input=“text” field required if checkbox was selected












0















i have a bootstrap formular which include two different addresses



The first address is the main address of the user and all fields are required.



The second address is the billing address and this field (street, street number, zip code and city) should be required if the user select the "billing address checkbox".



I work with http://formvalidation.io/ but i don't know how to get the dependence between checkbox and my billing address input fields.



Maybe someone can help?










share|improve this question























  • You can take a look here maybe it will help you formvalidation.io/examples/conditional-validation

    – Ionut
    Dec 12 '16 at 15:43
















0















i have a bootstrap formular which include two different addresses



The first address is the main address of the user and all fields are required.



The second address is the billing address and this field (street, street number, zip code and city) should be required if the user select the "billing address checkbox".



I work with http://formvalidation.io/ but i don't know how to get the dependence between checkbox and my billing address input fields.



Maybe someone can help?










share|improve this question























  • You can take a look here maybe it will help you formvalidation.io/examples/conditional-validation

    – Ionut
    Dec 12 '16 at 15:43














0












0








0








i have a bootstrap formular which include two different addresses



The first address is the main address of the user and all fields are required.



The second address is the billing address and this field (street, street number, zip code and city) should be required if the user select the "billing address checkbox".



I work with http://formvalidation.io/ but i don't know how to get the dependence between checkbox and my billing address input fields.



Maybe someone can help?










share|improve this question














i have a bootstrap formular which include two different addresses



The first address is the main address of the user and all fields are required.



The second address is the billing address and this field (street, street number, zip code and city) should be required if the user select the "billing address checkbox".



I work with http://formvalidation.io/ but i don't know how to get the dependence between checkbox and my billing address input fields.



Maybe someone can help?







jquery forms twitter-bootstrap validation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 12 '16 at 15:37









weristsamweristsam

791212




791212













  • You can take a look here maybe it will help you formvalidation.io/examples/conditional-validation

    – Ionut
    Dec 12 '16 at 15:43



















  • You can take a look here maybe it will help you formvalidation.io/examples/conditional-validation

    – Ionut
    Dec 12 '16 at 15:43

















You can take a look here maybe it will help you formvalidation.io/examples/conditional-validation

– Ionut
Dec 12 '16 at 15:43





You can take a look here maybe it will help you formvalidation.io/examples/conditional-validation

– Ionut
Dec 12 '16 at 15:43












2 Answers
2






active

oldest

votes


















4














You can add a jQuery .change() event listener to the checkbox which then toggles the required attribute on the text input:






$(document).ready(function() {

$('input[type="checkbox"]').change(function() {

if ($('input[type="text"]').attr('required')) {
$('input[type="text"]').removeAttr('required');
}

else {
$('input[type="text"]').attr('required','required');
}

});

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
<p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
<input type="text" />
<input type="submit" value="Submit" />
</form>







Here is the same approach in native javascript so that you can compare and contrast against the jQuery:






var checkBox = document.querySelector('input[type="checkbox"]');
var textInput = document.querySelector('input[type="text"]');

function toggleRequired() {

if (textInput.hasAttribute('required') !== true) {
textInput.setAttribute('required','required');
}

else {
textInput.removeAttribute('required');
}
}

checkBox.addEventListener('change',toggleRequired,false);

<form>
<p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
<input type="text" />
<input type="submit" value="Submit" />
</form>








share|improve this answer

































    1














    You can also check if the checkbox is actually checked rather than when it is clicked. It will work better for scenarios where you use already checked fields.



    let toggleInputRequired = ( checkbox, input ) => {
    checkbox.addEventListener( 'change', e => {
    if ( e.currentTarget.checked )
    input.setAttribute( 'required', 'required' );
    else
    input.removeAttribute( 'required' );
    } );

    checkbox.onchange();
    }

    toggleInputRequired( document.querySelector( 'input[name="checkbox_name"]' ), document.querySelector( 'input[name="input_text"]' ) );





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41104324%2fmake-input-text-field-required-if-checkbox-was-selected%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      You can add a jQuery .change() event listener to the checkbox which then toggles the required attribute on the text input:






      $(document).ready(function() {

      $('input[type="checkbox"]').change(function() {

      if ($('input[type="text"]').attr('required')) {
      $('input[type="text"]').removeAttr('required');
      }

      else {
      $('input[type="text"]').attr('required','required');
      }

      });

      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <form>
      <p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
      <input type="text" />
      <input type="submit" value="Submit" />
      </form>







      Here is the same approach in native javascript so that you can compare and contrast against the jQuery:






      var checkBox = document.querySelector('input[type="checkbox"]');
      var textInput = document.querySelector('input[type="text"]');

      function toggleRequired() {

      if (textInput.hasAttribute('required') !== true) {
      textInput.setAttribute('required','required');
      }

      else {
      textInput.removeAttribute('required');
      }
      }

      checkBox.addEventListener('change',toggleRequired,false);

      <form>
      <p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
      <input type="text" />
      <input type="submit" value="Submit" />
      </form>








      share|improve this answer






























        4














        You can add a jQuery .change() event listener to the checkbox which then toggles the required attribute on the text input:






        $(document).ready(function() {

        $('input[type="checkbox"]').change(function() {

        if ($('input[type="text"]').attr('required')) {
        $('input[type="text"]').removeAttr('required');
        }

        else {
        $('input[type="text"]').attr('required','required');
        }

        });

        });

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

        <form>
        <p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
        <input type="text" />
        <input type="submit" value="Submit" />
        </form>







        Here is the same approach in native javascript so that you can compare and contrast against the jQuery:






        var checkBox = document.querySelector('input[type="checkbox"]');
        var textInput = document.querySelector('input[type="text"]');

        function toggleRequired() {

        if (textInput.hasAttribute('required') !== true) {
        textInput.setAttribute('required','required');
        }

        else {
        textInput.removeAttribute('required');
        }
        }

        checkBox.addEventListener('change',toggleRequired,false);

        <form>
        <p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
        <input type="text" />
        <input type="submit" value="Submit" />
        </form>








        share|improve this answer




























          4












          4








          4







          You can add a jQuery .change() event listener to the checkbox which then toggles the required attribute on the text input:






          $(document).ready(function() {

          $('input[type="checkbox"]').change(function() {

          if ($('input[type="text"]').attr('required')) {
          $('input[type="text"]').removeAttr('required');
          }

          else {
          $('input[type="text"]').attr('required','required');
          }

          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

          <form>
          <p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>







          Here is the same approach in native javascript so that you can compare and contrast against the jQuery:






          var checkBox = document.querySelector('input[type="checkbox"]');
          var textInput = document.querySelector('input[type="text"]');

          function toggleRequired() {

          if (textInput.hasAttribute('required') !== true) {
          textInput.setAttribute('required','required');
          }

          else {
          textInput.removeAttribute('required');
          }
          }

          checkBox.addEventListener('change',toggleRequired,false);

          <form>
          <p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>








          share|improve this answer















          You can add a jQuery .change() event listener to the checkbox which then toggles the required attribute on the text input:






          $(document).ready(function() {

          $('input[type="checkbox"]').change(function() {

          if ($('input[type="text"]').attr('required')) {
          $('input[type="text"]').removeAttr('required');
          }

          else {
          $('input[type="text"]').attr('required','required');
          }

          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

          <form>
          <p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>







          Here is the same approach in native javascript so that you can compare and contrast against the jQuery:






          var checkBox = document.querySelector('input[type="checkbox"]');
          var textInput = document.querySelector('input[type="text"]');

          function toggleRequired() {

          if (textInput.hasAttribute('required') !== true) {
          textInput.setAttribute('required','required');
          }

          else {
          textInput.removeAttribute('required');
          }
          }

          checkBox.addEventListener('change',toggleRequired,false);

          <form>
          <p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>








          $(document).ready(function() {

          $('input[type="checkbox"]').change(function() {

          if ($('input[type="text"]').attr('required')) {
          $('input[type="text"]').removeAttr('required');
          }

          else {
          $('input[type="text"]').attr('required','required');
          }

          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

          <form>
          <p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>





          $(document).ready(function() {

          $('input[type="checkbox"]').change(function() {

          if ($('input[type="text"]').attr('required')) {
          $('input[type="text"]').removeAttr('required');
          }

          else {
          $('input[type="text"]').attr('required','required');
          }

          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

          <form>
          <p><input type="checkbox" /> Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>





          var checkBox = document.querySelector('input[type="checkbox"]');
          var textInput = document.querySelector('input[type="text"]');

          function toggleRequired() {

          if (textInput.hasAttribute('required') !== true) {
          textInput.setAttribute('required','required');
          }

          else {
          textInput.removeAttribute('required');
          }
          }

          checkBox.addEventListener('change',toggleRequired,false);

          <form>
          <p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>





          var checkBox = document.querySelector('input[type="checkbox"]');
          var textInput = document.querySelector('input[type="text"]');

          function toggleRequired() {

          if (textInput.hasAttribute('required') !== true) {
          textInput.setAttribute('required','required');
          }

          else {
          textInput.removeAttribute('required');
          }
          }

          checkBox.addEventListener('change',toggleRequired,false);

          <form>
          <p><input type="checkbox" />Check Me to make the Text Box a Required Field</p>
          <input type="text" />
          <input type="submit" value="Submit" />
          </form>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 12 '16 at 16:12

























          answered Dec 12 '16 at 15:58









          RouninRounin

          12.8k22445




          12.8k22445

























              1














              You can also check if the checkbox is actually checked rather than when it is clicked. It will work better for scenarios where you use already checked fields.



              let toggleInputRequired = ( checkbox, input ) => {
              checkbox.addEventListener( 'change', e => {
              if ( e.currentTarget.checked )
              input.setAttribute( 'required', 'required' );
              else
              input.removeAttribute( 'required' );
              } );

              checkbox.onchange();
              }

              toggleInputRequired( document.querySelector( 'input[name="checkbox_name"]' ), document.querySelector( 'input[name="input_text"]' ) );





              share|improve this answer




























                1














                You can also check if the checkbox is actually checked rather than when it is clicked. It will work better for scenarios where you use already checked fields.



                let toggleInputRequired = ( checkbox, input ) => {
                checkbox.addEventListener( 'change', e => {
                if ( e.currentTarget.checked )
                input.setAttribute( 'required', 'required' );
                else
                input.removeAttribute( 'required' );
                } );

                checkbox.onchange();
                }

                toggleInputRequired( document.querySelector( 'input[name="checkbox_name"]' ), document.querySelector( 'input[name="input_text"]' ) );





                share|improve this answer


























                  1












                  1








                  1







                  You can also check if the checkbox is actually checked rather than when it is clicked. It will work better for scenarios where you use already checked fields.



                  let toggleInputRequired = ( checkbox, input ) => {
                  checkbox.addEventListener( 'change', e => {
                  if ( e.currentTarget.checked )
                  input.setAttribute( 'required', 'required' );
                  else
                  input.removeAttribute( 'required' );
                  } );

                  checkbox.onchange();
                  }

                  toggleInputRequired( document.querySelector( 'input[name="checkbox_name"]' ), document.querySelector( 'input[name="input_text"]' ) );





                  share|improve this answer













                  You can also check if the checkbox is actually checked rather than when it is clicked. It will work better for scenarios where you use already checked fields.



                  let toggleInputRequired = ( checkbox, input ) => {
                  checkbox.addEventListener( 'change', e => {
                  if ( e.currentTarget.checked )
                  input.setAttribute( 'required', 'required' );
                  else
                  input.removeAttribute( 'required' );
                  } );

                  checkbox.onchange();
                  }

                  toggleInputRequired( document.querySelector( 'input[name="checkbox_name"]' ), document.querySelector( 'input[name="input_text"]' ) );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 16:44









                  Marek SapińskiMarek Sapiński

                  1045




                  1045






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41104324%2fmake-input-text-field-required-if-checkbox-was-selected%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Run scheduled task as local user group (not BUILTIN)

                      Port of Spain