Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I disable the submit button when the form gets submitted without any errors

I want to disable the submit button when a user submits the form so that he may not click the submit button twice.

So I coded the following javascript in my page


$(document).ready(function(){
    $("form").submit(function(){
       $("form").find('input[type=submit]').attr('disabled', 'disabled');
    });
})

This works great. But when I applied the jquery validate library and appended the following code


$(document).ready(function(){
    $("form").submit(function(){
       $("form").find('input[type=submit]').attr('disabled', 'disabled');
    });

    $("form").validate({
            errorClass: "jqueryError",
            errorElement: 'label',
            success: 'jqueryValid',
            messages: {
                TopicCategory: {
                    required: 'Please choose a category for topic.'
                },
                TopicSubcategoryId: {
                    required: 'Please choose a subcategory for topic.'
                },
                TopicTitle: {
                    required: 'Please enter a title for topic.'
                }
            }
        });
})

The submit button got disabled after submitting form even if there are incomplete inputs in the website (i.e. form with errors and user is asked to fill them properly).
and when the user completes the form correcting all errors the submit button is disabled.

How do I first check if there are no errors in the form and then disable the submit button and then submit it finally.

Thanks

like image 233
Gaurav Sharma Avatar asked May 07 '10 16:05

Gaurav Sharma


People also ask

How do you disable submit button on form submit?

Enable / Disable submit button 1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

How do you disable form submit button until all input fields are filled?

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 do I disable submit button in Servicenow?

Yes, you can hide submit button by adding a condition on Submit UI action in UI actions. To hide new buttons on related lists. you can click on configure -> list layout and omit new buttons.


1 Answers

Add the following parameter to jQuery Validate:

submitHandler: function(form) {
    $(':submit', form).attr('disabled', 'disabled');
    form.submit();
}
like image 131
SLaks Avatar answered Oct 05 '22 23:10

SLaks