0

I am trying to find out how can I disable the submit button until all form fields are filled. I have seen there are many solutions online in jQuery and JS.

But what I am looking for is, instead of checking all fields every time when there is a change in the field value, isn't there a way that checks the all the fields altogether at once at the end just like 'required' attribute?

For example, if you see the below solution provided here here.

$("input[type='text'], textarea").on("keyup", function(){

    // checking for all fields on "keyup"

    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");
    }
});

We can see it's checking for all fields every time on keyup or on change is called. But I need to know if there is any other efficient way rather than checking all fields every time on keyup or on change is called?

2
  • 1
    just like the required attribute - what is wrong with that Commented Jul 13, 2021 at 17:37
  • 1
    check jQuery Validator Commented Jul 13, 2021 at 18:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.