0

I have a simple contact form, but I want to disable the submit button until both inputs are not empty anymore. I read a lot of answers about kind of the same problem, but they all get answered in jQuery, but I would like to use native JavaScript. How can I achieve this? And can it check if the button should be enabled after any kind of change to the inputs, like pasting?

$(document).ready(function() {
    var $submit = $("#verzend"),
        $inputs = $('#textarea, #email');
    function checkEmpty() {
        return $inputs.filter(function() {
            return !$.trim(this.value);
        }).length === 0;
    }
    $inputs.on('keyup blur', function() {
        $submit.prop("disabled", !checkEmpty());
    }).keyup();
});

I found this as a good solution, but it is in JQuery

1
  • 1
    Maybe add what you have so we can help Commented Feb 7, 2018 at 20:18

1 Answer 1

2

You can easily do this with the required attribute. Place this on both of your input. The browser should block this automatically.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah, I already have that, but these could be easily removed in the inspector of the browser.
@MrkSmt anything can be undone by the client. you need to validate on the server too.
I do, but it for my project it is necessary to add en remove the 'disabled' attribute for my submit button.

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.