Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable submit button only when all fields are filled

Tags:

html

jquery

forms

In my form, I want to enable form only when all fields (and radio button list) has been selected.

So far, I have successfully made the submit button enable when the title field has content with:

onkeyup="if(this.textLength != 0) {subnewtopic.disabled = false} else {subnewtopic.disabled = true}"

My aim is to enable submit button only once everything has been filled. How do I do this?

Here is my full code with working jsfiddle:

            <form action="" method="post" id="subnewtopicform" />

                Title:
                <input type="text" name="title" onkeyup="if(this.textLength != 0) {subnewtopic.disabled = false} else {subnewtopic.disabled = true}">

<br/>

                Description:
                <textarea name="description"></textarea> 

<br/>
                Category:
<ul class="list:category categorychecklist form-no-clear" id="categorychecklist">

<li id="category-19"><label class="selectit"><input type="radio" id="in-category-19" name="category" value="19"> Animation</label></li>

<li id="category-20"><label class="selectit"><input type="radio" id="in-category-20" name="category" value="20"> Anime</label></li>

    </ul>

                <input type="submit" value="Submit Topic" class="button-primary" name="subnewtopic" id="subnewtopic" disabled="disabled" /> 

            </form>
like image 359
Henrik Petterson Avatar asked Jan 07 '15 21:01

Henrik Petterson


People also ask

How do you disable submit button until all fields are entered?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How can I make a button go to the next page only if all form fields are not empty?

The way to usually do it, is combine normal form submission with POST -Redirect- GET (the server accepts the form submission, then redirects the page). After the form submission is successful, redirect the page from the server-side to the next page (or to an error page, in case something went wrong).

How do I enable a submit button?

Approach: To enable or disable the form submit button, we use the below code snippet. $('#enabled'). click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button').

How do you disable submit button until form is filled react?

To disable a button when an input is empty with React, we can set the input's value as a state's value. Then we can use the state to conditionally disable the button when the state's value is empty. to create the name state with the useState hook.


3 Answers

Here is a fiddle for you. http://jsfiddle.net/soyn0xag/6/

$("input[type='text'], textarea").on("keyup", function(){
    if($(this).val() != "" && $("textarea").val() != "" && $("input[name='category']").is(":checked") == true){
        $("input[type='submit']").removeAttr("disabled");
    } else {
        $("input[type='submit']").attr("disabled", "disabled");
    }
});

$("input[name='category']").on("change", function(){
    if($(this).val() != "" && $("textarea").val() != "" && $("input[name='category']").is(":checked") == true){
        $("input[type='submit']").removeAttr("disabled");
    } else {
        $("input[type='submit']").attr("disabled", "disabled");
    }
});
like image 76
eg_dac Avatar answered Oct 07 '22 17:10

eg_dac


use the "required" command inside the tag.

edit: Even with the radio buttons. you'd only have to select one. Here is the JSFiddle: http://jsfiddle.net/soyn0xag/3/

edit 2: Added a new working JSfiddle with your request to keep the submit button disabled until all fields have content. I used jquery, and the submit button is available after all typable fields have content, and the radio buttons are still required with that nice pop up: http://jsfiddle.net/soyn0xag/9/

this option DISABLES the button again once you take away the content from one of the fields. I believe this is what you want.

edit 3 - Final Fix Fixed a bug that let the submit button show up early: http://jsfiddle.net/soyn0xag/36/

As long as the and the submit button are wrapped in the same tag, you will not be able to submit without content in the now required input fields.

Example:

<input type="radio" id="in-category-19" name="category" value="19" required> Animation</label>

Note that required does not use quotes or any other delimiter.

like image 43
Sanova Avatar answered Oct 07 '22 17:10

Sanova


yet another solution

required = function(fields) {
  var valid = true;
  fields.each(function () { // iterate all
    var $this = $(this);
    if (($this.is(':checkbox') && !$this.is(":checked")) || // checkbox
       (($this.is(':text') || $this.is('textarea')) && !$this.val()) || // text 
       ($this.is(':radio') && !$('input[name='+ $this.attr("name") +']:checked').length)) {
            valid = false;
        }
    });
    return valid;
}

validateRealTime = function () {
    var fields = $("form :input:not(:hidden)"); // select required
    fields.on('keyup change keypress blur', function () {
        if (required(fields)) {
            {subnewtopic.disabled = false} // action if all valid
        } else {
            {subnewtopic.disabled = true} // action if not valid
        }
    });
}

http://jsfiddle.net/jsq7m8hL/2/

like image 3
Master Slave Avatar answered Oct 07 '22 17:10

Master Slave