Simplify form validation with function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite
1












In jQuery, I have been reluctant to create a better form validation, and I feel the time has come for me to do so.



With that said, I have been using this particular type of form validation, and I would like to simplify it.



$('#saveSubmit').on('click', function()

var criteria =

company: $('#company').val(),
partnercode: $('#partnercode').val(),
office: $('#office').val(),
// few more


// form validation starts here
if(criteria.company == "")

$('#compError').show();
return false;

if(criteria.partnercode == "")

$('#compError').show();
return false;

if(criteria.office == "")

$('#compError').show();
return false;

// few more if statements
else

// if the form parameters are good, send to processing script
$.post('process/editUser.php', criteria:criteria, function(data)

if(data.indexOf("Error") >= 0)

$('#errorModal').modal('show');
$('#message').text(data);
return false;

else

$('#successModal').modal('show');
$('#messageSuccess').text(data);
$('#successModal').on('hidden.bs.modal', function()

$('#successModal').modal('hide');
location.reload();
);

);

);


All of this works like how it should, but I want to make it less verbose, thus the need for a function.



I want to replace that huge if/else statement within that click event with a function.







share|improve this question



























    up vote
    3
    down vote

    favorite
    1












    In jQuery, I have been reluctant to create a better form validation, and I feel the time has come for me to do so.



    With that said, I have been using this particular type of form validation, and I would like to simplify it.



    $('#saveSubmit').on('click', function()

    var criteria =

    company: $('#company').val(),
    partnercode: $('#partnercode').val(),
    office: $('#office').val(),
    // few more


    // form validation starts here
    if(criteria.company == "")

    $('#compError').show();
    return false;

    if(criteria.partnercode == "")

    $('#compError').show();
    return false;

    if(criteria.office == "")

    $('#compError').show();
    return false;

    // few more if statements
    else

    // if the form parameters are good, send to processing script
    $.post('process/editUser.php', criteria:criteria, function(data)

    if(data.indexOf("Error") >= 0)

    $('#errorModal').modal('show');
    $('#message').text(data);
    return false;

    else

    $('#successModal').modal('show');
    $('#messageSuccess').text(data);
    $('#successModal').on('hidden.bs.modal', function()

    $('#successModal').modal('hide');
    location.reload();
    );

    );

    );


    All of this works like how it should, but I want to make it less verbose, thus the need for a function.



    I want to replace that huge if/else statement within that click event with a function.







    share|improve this question























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      In jQuery, I have been reluctant to create a better form validation, and I feel the time has come for me to do so.



      With that said, I have been using this particular type of form validation, and I would like to simplify it.



      $('#saveSubmit').on('click', function()

      var criteria =

      company: $('#company').val(),
      partnercode: $('#partnercode').val(),
      office: $('#office').val(),
      // few more


      // form validation starts here
      if(criteria.company == "")

      $('#compError').show();
      return false;

      if(criteria.partnercode == "")

      $('#compError').show();
      return false;

      if(criteria.office == "")

      $('#compError').show();
      return false;

      // few more if statements
      else

      // if the form parameters are good, send to processing script
      $.post('process/editUser.php', criteria:criteria, function(data)

      if(data.indexOf("Error") >= 0)

      $('#errorModal').modal('show');
      $('#message').text(data);
      return false;

      else

      $('#successModal').modal('show');
      $('#messageSuccess').text(data);
      $('#successModal').on('hidden.bs.modal', function()

      $('#successModal').modal('hide');
      location.reload();
      );

      );

      );


      All of this works like how it should, but I want to make it less verbose, thus the need for a function.



      I want to replace that huge if/else statement within that click event with a function.







      share|improve this question













      In jQuery, I have been reluctant to create a better form validation, and I feel the time has come for me to do so.



      With that said, I have been using this particular type of form validation, and I would like to simplify it.



      $('#saveSubmit').on('click', function()

      var criteria =

      company: $('#company').val(),
      partnercode: $('#partnercode').val(),
      office: $('#office').val(),
      // few more


      // form validation starts here
      if(criteria.company == "")

      $('#compError').show();
      return false;

      if(criteria.partnercode == "")

      $('#compError').show();
      return false;

      if(criteria.office == "")

      $('#compError').show();
      return false;

      // few more if statements
      else

      // if the form parameters are good, send to processing script
      $.post('process/editUser.php', criteria:criteria, function(data)

      if(data.indexOf("Error") >= 0)

      $('#errorModal').modal('show');
      $('#message').text(data);
      return false;

      else

      $('#successModal').modal('show');
      $('#messageSuccess').text(data);
      $('#successModal').on('hidden.bs.modal', function()

      $('#successModal').modal('hide');
      location.reload();
      );

      );

      );


      All of this works like how it should, but I want to make it less verbose, thus the need for a function.



      I want to replace that huge if/else statement within that click event with a function.









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 15 at 2:33









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked May 14 at 15:39









      John Beasley

      1704




      1704




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          4
          down vote













          You might break apart the code in separate functions, and create a function that opens and closes a modal with a custom text. I didn't check this code, hopefully you get some ideas for improvement!



          $('#saveSubmit').on('click',()=>validateForm())

          function validateForm()

          var criteria =

          company: $('#company').val(),
          partnercode: $('#partnercode').val(),
          office: $('#office').val(),
          // few more


          let error = ""
          if(criteria.company == "")

          error += "No company specified";

          if(criteria.partnercode == "")

          error += "No partner code specified";


          if(error == "")
          postData(criteria);
          else
          showModal(error);




          function postData(criteria)
          $.post('process/editUser.php', criteria:criteria,(data)=>checkData())


          function checkData(data)
          if(data.indexOf("Error") >= 0)
          showModal("error " + data, false);
          else
          showModal("data posted!", true);



          function showModal(content, button)

          $('#modal').show();
          $('#message').text(content);
          if(button)
          $('#modalbutton').show();
          $('#modalbutton').on('click',() => hideModal());



          function hideModal
          $('#modal').hide();






          share|improve this answer




























            up vote
            4
            down vote













            The obvious improvement to make it less verbose, if all your if statement just check an empty string (that's why it's asked to post all your code here btw ^^), is to use a loop instead, for example :



            for(let attr in criteria)
            if(criteria[attr] == "")

            $('#compError').show();
            return false;




            Also, since return false; shut the current function execution, you don't need an else around your $post (and it wouldn't fit with this new loop system anyway).






            share|improve this answer




























              up vote
              2
              down vote













              You can set a class for each field that is required. Then, on form validate, loop through all the required fields. If you find one that is empty, go into your routine. Here's an example, which assumes that all required elements have the class required:



              function validate() 
              var q = document.querySelectorAll('.required');
              for (var i=0; i<q.length; i++)
              if ('' == q[i].value)
              $('#compError').show();
              return false;


              return true;



              From a UX standpoint, I would consider focusing on the offending element, and showing a message saying that the specific field is required.






              share|improve this answer




























                up vote
                2
                down vote













                The question originally was tagged with functional-programming, but it seems more declarative/procedural than functional, other than the fact that callback functions are passed to the click handlers.



                The answer by Nomis is good: one can iterate over the values in criteria and as soon as a blank value is found, show the error message container. A functional approach for this would be to use Object.values() to get the array of values in criteria and then utilize Array.every() to look for empty values:



                var allNonEmpty = Object.values(criteria).every(function(entry) 
                return entry.length;
                );
                if (!allNonEmpty)
                $('#compError').show();



                If you are supporting ES-2015 (A.K.A. es-6) then an arrow function can make that shorter:



                const allNonEmpty = Object.values(criteria).every(entry => entry.length);


                It is also advisable to cache DOM lookups instead of performing them each time. So when the page loads, it might be best to store a reference to each input in a variable (or, if using es-6, a constant using const). See the expandable snippet below for an example.






                $(function() //jQuery DOM-ready callback
                var inputFields =
                company: $('#company'),
                partnercode: $('#partnercode'),
                office: $('#office')
                ;
                var compError = $('#compError');
                $('#saveSubmit').on('click', function()
                var allNonEmpty = Object.values(inputFields).every(function(inputField)
                return $(inputField).val().length;
                );
                if (!allNonEmpty)
                compError.show();
                return;

                else
                compError.hide();
                //AJAX code

                );
                );

                .hidden 
                display: none;

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                <div id="banner-message">
                <pThe Form</p>
                <div>
                Company: <input id="company" />
                </div>
                <div>
                Partner: <input id="partnercode" />
                </div>
                <div>
                office: <input id="office" />
                </div>
                <button id="saveSubmit">submit</button>
                <div id="compError" class="hidden">
                Error!
                </div>
                </div>








                share|improve this answer























                  Your Answer




                  StackExchange.ifUsing("editor", function ()
                  return StackExchange.using("mathjaxEditing", function ()
                  StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                  StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                  );
                  );
                  , "mathjax-editing");

                  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: "196"
                  ;
                  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',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );








                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194368%2fsimplify-form-validation-with-function%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  4
                  down vote













                  You might break apart the code in separate functions, and create a function that opens and closes a modal with a custom text. I didn't check this code, hopefully you get some ideas for improvement!



                  $('#saveSubmit').on('click',()=>validateForm())

                  function validateForm()

                  var criteria =

                  company: $('#company').val(),
                  partnercode: $('#partnercode').val(),
                  office: $('#office').val(),
                  // few more


                  let error = ""
                  if(criteria.company == "")

                  error += "No company specified";

                  if(criteria.partnercode == "")

                  error += "No partner code specified";


                  if(error == "")
                  postData(criteria);
                  else
                  showModal(error);




                  function postData(criteria)
                  $.post('process/editUser.php', criteria:criteria,(data)=>checkData())


                  function checkData(data)
                  if(data.indexOf("Error") >= 0)
                  showModal("error " + data, false);
                  else
                  showModal("data posted!", true);



                  function showModal(content, button)

                  $('#modal').show();
                  $('#message').text(content);
                  if(button)
                  $('#modalbutton').show();
                  $('#modalbutton').on('click',() => hideModal());



                  function hideModal
                  $('#modal').hide();






                  share|improve this answer

























                    up vote
                    4
                    down vote













                    You might break apart the code in separate functions, and create a function that opens and closes a modal with a custom text. I didn't check this code, hopefully you get some ideas for improvement!



                    $('#saveSubmit').on('click',()=>validateForm())

                    function validateForm()

                    var criteria =

                    company: $('#company').val(),
                    partnercode: $('#partnercode').val(),
                    office: $('#office').val(),
                    // few more


                    let error = ""
                    if(criteria.company == "")

                    error += "No company specified";

                    if(criteria.partnercode == "")

                    error += "No partner code specified";


                    if(error == "")
                    postData(criteria);
                    else
                    showModal(error);




                    function postData(criteria)
                    $.post('process/editUser.php', criteria:criteria,(data)=>checkData())


                    function checkData(data)
                    if(data.indexOf("Error") >= 0)
                    showModal("error " + data, false);
                    else
                    showModal("data posted!", true);



                    function showModal(content, button)

                    $('#modal').show();
                    $('#message').text(content);
                    if(button)
                    $('#modalbutton').show();
                    $('#modalbutton').on('click',() => hideModal());



                    function hideModal
                    $('#modal').hide();






                    share|improve this answer























                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      You might break apart the code in separate functions, and create a function that opens and closes a modal with a custom text. I didn't check this code, hopefully you get some ideas for improvement!



                      $('#saveSubmit').on('click',()=>validateForm())

                      function validateForm()

                      var criteria =

                      company: $('#company').val(),
                      partnercode: $('#partnercode').val(),
                      office: $('#office').val(),
                      // few more


                      let error = ""
                      if(criteria.company == "")

                      error += "No company specified";

                      if(criteria.partnercode == "")

                      error += "No partner code specified";


                      if(error == "")
                      postData(criteria);
                      else
                      showModal(error);




                      function postData(criteria)
                      $.post('process/editUser.php', criteria:criteria,(data)=>checkData())


                      function checkData(data)
                      if(data.indexOf("Error") >= 0)
                      showModal("error " + data, false);
                      else
                      showModal("data posted!", true);



                      function showModal(content, button)

                      $('#modal').show();
                      $('#message').text(content);
                      if(button)
                      $('#modalbutton').show();
                      $('#modalbutton').on('click',() => hideModal());



                      function hideModal
                      $('#modal').hide();






                      share|improve this answer













                      You might break apart the code in separate functions, and create a function that opens and closes a modal with a custom text. I didn't check this code, hopefully you get some ideas for improvement!



                      $('#saveSubmit').on('click',()=>validateForm())

                      function validateForm()

                      var criteria =

                      company: $('#company').val(),
                      partnercode: $('#partnercode').val(),
                      office: $('#office').val(),
                      // few more


                      let error = ""
                      if(criteria.company == "")

                      error += "No company specified";

                      if(criteria.partnercode == "")

                      error += "No partner code specified";


                      if(error == "")
                      postData(criteria);
                      else
                      showModal(error);




                      function postData(criteria)
                      $.post('process/editUser.php', criteria:criteria,(data)=>checkData())


                      function checkData(data)
                      if(data.indexOf("Error") >= 0)
                      showModal("error " + data, false);
                      else
                      showModal("data posted!", true);



                      function showModal(content, button)

                      $('#modal').show();
                      $('#message').text(content);
                      if(button)
                      $('#modalbutton').show();
                      $('#modalbutton').on('click',() => hideModal());



                      function hideModal
                      $('#modal').hide();







                      share|improve this answer













                      share|improve this answer



                      share|improve this answer











                      answered May 14 at 15:57









                      Kokodoko

                      1815




                      1815






















                          up vote
                          4
                          down vote













                          The obvious improvement to make it less verbose, if all your if statement just check an empty string (that's why it's asked to post all your code here btw ^^), is to use a loop instead, for example :



                          for(let attr in criteria)
                          if(criteria[attr] == "")

                          $('#compError').show();
                          return false;




                          Also, since return false; shut the current function execution, you don't need an else around your $post (and it wouldn't fit with this new loop system anyway).






                          share|improve this answer

























                            up vote
                            4
                            down vote













                            The obvious improvement to make it less verbose, if all your if statement just check an empty string (that's why it's asked to post all your code here btw ^^), is to use a loop instead, for example :



                            for(let attr in criteria)
                            if(criteria[attr] == "")

                            $('#compError').show();
                            return false;




                            Also, since return false; shut the current function execution, you don't need an else around your $post (and it wouldn't fit with this new loop system anyway).






                            share|improve this answer























                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              The obvious improvement to make it less verbose, if all your if statement just check an empty string (that's why it's asked to post all your code here btw ^^), is to use a loop instead, for example :



                              for(let attr in criteria)
                              if(criteria[attr] == "")

                              $('#compError').show();
                              return false;




                              Also, since return false; shut the current function execution, you don't need an else around your $post (and it wouldn't fit with this new loop system anyway).






                              share|improve this answer













                              The obvious improvement to make it less verbose, if all your if statement just check an empty string (that's why it's asked to post all your code here btw ^^), is to use a loop instead, for example :



                              for(let attr in criteria)
                              if(criteria[attr] == "")

                              $('#compError').show();
                              return false;




                              Also, since return false; shut the current function execution, you don't need an else around your $post (and it wouldn't fit with this new loop system anyway).







                              share|improve this answer













                              share|improve this answer



                              share|improve this answer











                              answered May 14 at 16:00









                              Nomis

                              1615




                              1615




















                                  up vote
                                  2
                                  down vote













                                  You can set a class for each field that is required. Then, on form validate, loop through all the required fields. If you find one that is empty, go into your routine. Here's an example, which assumes that all required elements have the class required:



                                  function validate() 
                                  var q = document.querySelectorAll('.required');
                                  for (var i=0; i<q.length; i++)
                                  if ('' == q[i].value)
                                  $('#compError').show();
                                  return false;


                                  return true;



                                  From a UX standpoint, I would consider focusing on the offending element, and showing a message saying that the specific field is required.






                                  share|improve this answer

























                                    up vote
                                    2
                                    down vote













                                    You can set a class for each field that is required. Then, on form validate, loop through all the required fields. If you find one that is empty, go into your routine. Here's an example, which assumes that all required elements have the class required:



                                    function validate() 
                                    var q = document.querySelectorAll('.required');
                                    for (var i=0; i<q.length; i++)
                                    if ('' == q[i].value)
                                    $('#compError').show();
                                    return false;


                                    return true;



                                    From a UX standpoint, I would consider focusing on the offending element, and showing a message saying that the specific field is required.






                                    share|improve this answer























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      You can set a class for each field that is required. Then, on form validate, loop through all the required fields. If you find one that is empty, go into your routine. Here's an example, which assumes that all required elements have the class required:



                                      function validate() 
                                      var q = document.querySelectorAll('.required');
                                      for (var i=0; i<q.length; i++)
                                      if ('' == q[i].value)
                                      $('#compError').show();
                                      return false;


                                      return true;



                                      From a UX standpoint, I would consider focusing on the offending element, and showing a message saying that the specific field is required.






                                      share|improve this answer













                                      You can set a class for each field that is required. Then, on form validate, loop through all the required fields. If you find one that is empty, go into your routine. Here's an example, which assumes that all required elements have the class required:



                                      function validate() 
                                      var q = document.querySelectorAll('.required');
                                      for (var i=0; i<q.length; i++)
                                      if ('' == q[i].value)
                                      $('#compError').show();
                                      return false;


                                      return true;



                                      From a UX standpoint, I would consider focusing on the offending element, and showing a message saying that the specific field is required.







                                      share|improve this answer













                                      share|improve this answer



                                      share|improve this answer











                                      answered May 19 at 20:51









                                      BobRodes

                                      1212




                                      1212




















                                          up vote
                                          2
                                          down vote













                                          The question originally was tagged with functional-programming, but it seems more declarative/procedural than functional, other than the fact that callback functions are passed to the click handlers.



                                          The answer by Nomis is good: one can iterate over the values in criteria and as soon as a blank value is found, show the error message container. A functional approach for this would be to use Object.values() to get the array of values in criteria and then utilize Array.every() to look for empty values:



                                          var allNonEmpty = Object.values(criteria).every(function(entry) 
                                          return entry.length;
                                          );
                                          if (!allNonEmpty)
                                          $('#compError').show();



                                          If you are supporting ES-2015 (A.K.A. es-6) then an arrow function can make that shorter:



                                          const allNonEmpty = Object.values(criteria).every(entry => entry.length);


                                          It is also advisable to cache DOM lookups instead of performing them each time. So when the page loads, it might be best to store a reference to each input in a variable (or, if using es-6, a constant using const). See the expandable snippet below for an example.






                                          $(function() //jQuery DOM-ready callback
                                          var inputFields =
                                          company: $('#company'),
                                          partnercode: $('#partnercode'),
                                          office: $('#office')
                                          ;
                                          var compError = $('#compError');
                                          $('#saveSubmit').on('click', function()
                                          var allNonEmpty = Object.values(inputFields).every(function(inputField)
                                          return $(inputField).val().length;
                                          );
                                          if (!allNonEmpty)
                                          compError.show();
                                          return;

                                          else
                                          compError.hide();
                                          //AJAX code

                                          );
                                          );

                                          .hidden 
                                          display: none;

                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                                          <div id="banner-message">
                                          <pThe Form</p>
                                          <div>
                                          Company: <input id="company" />
                                          </div>
                                          <div>
                                          Partner: <input id="partnercode" />
                                          </div>
                                          <div>
                                          office: <input id="office" />
                                          </div>
                                          <button id="saveSubmit">submit</button>
                                          <div id="compError" class="hidden">
                                          Error!
                                          </div>
                                          </div>








                                          share|improve this answer



























                                            up vote
                                            2
                                            down vote













                                            The question originally was tagged with functional-programming, but it seems more declarative/procedural than functional, other than the fact that callback functions are passed to the click handlers.



                                            The answer by Nomis is good: one can iterate over the values in criteria and as soon as a blank value is found, show the error message container. A functional approach for this would be to use Object.values() to get the array of values in criteria and then utilize Array.every() to look for empty values:



                                            var allNonEmpty = Object.values(criteria).every(function(entry) 
                                            return entry.length;
                                            );
                                            if (!allNonEmpty)
                                            $('#compError').show();



                                            If you are supporting ES-2015 (A.K.A. es-6) then an arrow function can make that shorter:



                                            const allNonEmpty = Object.values(criteria).every(entry => entry.length);


                                            It is also advisable to cache DOM lookups instead of performing them each time. So when the page loads, it might be best to store a reference to each input in a variable (or, if using es-6, a constant using const). See the expandable snippet below for an example.






                                            $(function() //jQuery DOM-ready callback
                                            var inputFields =
                                            company: $('#company'),
                                            partnercode: $('#partnercode'),
                                            office: $('#office')
                                            ;
                                            var compError = $('#compError');
                                            $('#saveSubmit').on('click', function()
                                            var allNonEmpty = Object.values(inputFields).every(function(inputField)
                                            return $(inputField).val().length;
                                            );
                                            if (!allNonEmpty)
                                            compError.show();
                                            return;

                                            else
                                            compError.hide();
                                            //AJAX code

                                            );
                                            );

                                            .hidden 
                                            display: none;

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                                            <div id="banner-message">
                                            <pThe Form</p>
                                            <div>
                                            Company: <input id="company" />
                                            </div>
                                            <div>
                                            Partner: <input id="partnercode" />
                                            </div>
                                            <div>
                                            office: <input id="office" />
                                            </div>
                                            <button id="saveSubmit">submit</button>
                                            <div id="compError" class="hidden">
                                            Error!
                                            </div>
                                            </div>








                                            share|improve this answer

























                                              up vote
                                              2
                                              down vote










                                              up vote
                                              2
                                              down vote









                                              The question originally was tagged with functional-programming, but it seems more declarative/procedural than functional, other than the fact that callback functions are passed to the click handlers.



                                              The answer by Nomis is good: one can iterate over the values in criteria and as soon as a blank value is found, show the error message container. A functional approach for this would be to use Object.values() to get the array of values in criteria and then utilize Array.every() to look for empty values:



                                              var allNonEmpty = Object.values(criteria).every(function(entry) 
                                              return entry.length;
                                              );
                                              if (!allNonEmpty)
                                              $('#compError').show();



                                              If you are supporting ES-2015 (A.K.A. es-6) then an arrow function can make that shorter:



                                              const allNonEmpty = Object.values(criteria).every(entry => entry.length);


                                              It is also advisable to cache DOM lookups instead of performing them each time. So when the page loads, it might be best to store a reference to each input in a variable (or, if using es-6, a constant using const). See the expandable snippet below for an example.






                                              $(function() //jQuery DOM-ready callback
                                              var inputFields =
                                              company: $('#company'),
                                              partnercode: $('#partnercode'),
                                              office: $('#office')
                                              ;
                                              var compError = $('#compError');
                                              $('#saveSubmit').on('click', function()
                                              var allNonEmpty = Object.values(inputFields).every(function(inputField)
                                              return $(inputField).val().length;
                                              );
                                              if (!allNonEmpty)
                                              compError.show();
                                              return;

                                              else
                                              compError.hide();
                                              //AJAX code

                                              );
                                              );

                                              .hidden 
                                              display: none;

                                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                                              <div id="banner-message">
                                              <pThe Form</p>
                                              <div>
                                              Company: <input id="company" />
                                              </div>
                                              <div>
                                              Partner: <input id="partnercode" />
                                              </div>
                                              <div>
                                              office: <input id="office" />
                                              </div>
                                              <button id="saveSubmit">submit</button>
                                              <div id="compError" class="hidden">
                                              Error!
                                              </div>
                                              </div>








                                              share|improve this answer















                                              The question originally was tagged with functional-programming, but it seems more declarative/procedural than functional, other than the fact that callback functions are passed to the click handlers.



                                              The answer by Nomis is good: one can iterate over the values in criteria and as soon as a blank value is found, show the error message container. A functional approach for this would be to use Object.values() to get the array of values in criteria and then utilize Array.every() to look for empty values:



                                              var allNonEmpty = Object.values(criteria).every(function(entry) 
                                              return entry.length;
                                              );
                                              if (!allNonEmpty)
                                              $('#compError').show();



                                              If you are supporting ES-2015 (A.K.A. es-6) then an arrow function can make that shorter:



                                              const allNonEmpty = Object.values(criteria).every(entry => entry.length);


                                              It is also advisable to cache DOM lookups instead of performing them each time. So when the page loads, it might be best to store a reference to each input in a variable (or, if using es-6, a constant using const). See the expandable snippet below for an example.






                                              $(function() //jQuery DOM-ready callback
                                              var inputFields =
                                              company: $('#company'),
                                              partnercode: $('#partnercode'),
                                              office: $('#office')
                                              ;
                                              var compError = $('#compError');
                                              $('#saveSubmit').on('click', function()
                                              var allNonEmpty = Object.values(inputFields).every(function(inputField)
                                              return $(inputField).val().length;
                                              );
                                              if (!allNonEmpty)
                                              compError.show();
                                              return;

                                              else
                                              compError.hide();
                                              //AJAX code

                                              );
                                              );

                                              .hidden 
                                              display: none;

                                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                                              <div id="banner-message">
                                              <pThe Form</p>
                                              <div>
                                              Company: <input id="company" />
                                              </div>
                                              <div>
                                              Partner: <input id="partnercode" />
                                              </div>
                                              <div>
                                              office: <input id="office" />
                                              </div>
                                              <button id="saveSubmit">submit</button>
                                              <div id="compError" class="hidden">
                                              Error!
                                              </div>
                                              </div>








                                              $(function() //jQuery DOM-ready callback
                                              var inputFields =
                                              company: $('#company'),
                                              partnercode: $('#partnercode'),
                                              office: $('#office')
                                              ;
                                              var compError = $('#compError');
                                              $('#saveSubmit').on('click', function()
                                              var allNonEmpty = Object.values(inputFields).every(function(inputField)
                                              return $(inputField).val().length;
                                              );
                                              if (!allNonEmpty)
                                              compError.show();
                                              return;

                                              else
                                              compError.hide();
                                              //AJAX code

                                              );
                                              );

                                              .hidden 
                                              display: none;

                                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                                              <div id="banner-message">
                                              <pThe Form</p>
                                              <div>
                                              Company: <input id="company" />
                                              </div>
                                              <div>
                                              Partner: <input id="partnercode" />
                                              </div>
                                              <div>
                                              office: <input id="office" />
                                              </div>
                                              <button id="saveSubmit">submit</button>
                                              <div id="compError" class="hidden">
                                              Error!
                                              </div>
                                              </div>





                                              $(function() //jQuery DOM-ready callback
                                              var inputFields =
                                              company: $('#company'),
                                              partnercode: $('#partnercode'),
                                              office: $('#office')
                                              ;
                                              var compError = $('#compError');
                                              $('#saveSubmit').on('click', function()
                                              var allNonEmpty = Object.values(inputFields).every(function(inputField)
                                              return $(inputField).val().length;
                                              );
                                              if (!allNonEmpty)
                                              compError.show();
                                              return;

                                              else
                                              compError.hide();
                                              //AJAX code

                                              );
                                              );

                                              .hidden 
                                              display: none;

                                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                                              <div id="banner-message">
                                              <pThe Form</p>
                                              <div>
                                              Company: <input id="company" />
                                              </div>
                                              <div>
                                              Partner: <input id="partnercode" />
                                              </div>
                                              <div>
                                              office: <input id="office" />
                                              </div>
                                              <button id="saveSubmit">submit</button>
                                              <div id="compError" class="hidden">
                                              Error!
                                              </div>
                                              </div>






                                              share|improve this answer















                                              share|improve this answer



                                              share|improve this answer








                                              edited May 19 at 23:09


























                                              answered May 14 at 21:08









                                              Sam Onela

                                              5,77461543




                                              5,77461543






















                                                   

                                                  draft saved


                                                  draft discarded


























                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194368%2fsimplify-form-validation-with-function%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Popular posts from this blog

                                                  Chat program with C++ and SFML

                                                  Function to Return a JSON Like Objects Using VBA Collections and Arrays

                                                  Will my employers contract hold up in court?