Force user to fill all fields before enabling form submit

前端 未结 12 2050
忘掉有多难
忘掉有多难 2021-02-07 00:49

I have a form containing various fields.

See jsFiddle demo.

My aim is to enable the submit button only when the user has filled in all fields.

So far,

12条回答
  •  渐次进展
    2021-02-07 01:39

    • Make the changes take effect after changing inputs values:

    On each input change, test the values of other inputs and checked state of radio, if all inputs has been entered it will make the submit button enabled:

    var validateInputs = function validateInputs(inputs) {
      var validForm = true;
      inputs.each(function(index) {
        var input = $(this);
        if (!input.val() || (input.type === "radio" && !input.is(':checked'))) {
          $("#subnewtide").attr("disabled", "disabled");
          validForm = false;
        }
      });
      return validForm;
    }
    
    inputs.change(function() {
      if (validateInputs(inputs)) {
        $("#subnewtide").removeAttr("disabled");
      }
    });
    

    Demo:

    var inputs = $("form#myForm input, form#myForm textarea");
    
    var validateInputs = function validateInputs(inputs) {
      var validForm = true;
      inputs.each(function(index) {
        var input = $(this);
        if (!input.val() || (input.type === "radio" && !input.is(':checked'))) {
          $("#subnewtide").attr("disabled", "disabled");
          validForm = false;
        }
      });
      return validForm;
    }
    
    
    inputs.change(function() {
      if (validateInputs(inputs)) {
        $("#subnewtide").removeAttr("disabled");
      }
    });
    
    
    Title:
    Description:
    Tag:
    Category: Animation

    Also it uses the form id="myForm", so you can use it to validate only specific forms in your pages.

    Note: This is tested and working on Chrome, Firefox and IE.


    EDIT:

    • Make the changes take effect when we type in the inputs:

    In the previous code we are using onchange event handler to call the function so it's only called when we click outside a given input (after change).

    To perform the call automatically when the user enters a character in a field (the last one) we need to use the onkeyup event so we don't need to click outside of it.

    This is the changed code you need :

        var inputs = $("form#myForm input, form#myForm textarea");
    
        var validateInputs = function validateInputs(inputs) {
          var validForm = true;
          inputs.each(function(index) {
            var input = $(this);
            if (!input.val() || (input.type === "radio" && !input.is(':checked'))) {
              $("#subnewtide").attr("disabled", "disabled");
              validForm = false;
            }
          });
          return validForm;
        }
    
    
        inputs.each(function() {
          var input = $(this);
          if (input.type === "radio") {
            input.change(function() {
              if (validateInputs(inputs)) {
                $("#subnewtide").removeAttr("disabled");
              }
            });
          } else {
            input.keyup(function() {
              if (validateInputs(inputs)) {
                $("#subnewtide").removeAttr("disabled");
              }
            });
          }
        });
    
    
    Title:
    Description:
    Tag:
    Category: Animation

提交回复
热议问题