Force user to fill all fields before enabling form submit

前端 未结 12 2039
忘掉有多难
忘掉有多难 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:38

    Why don't you use jquery validate . It's a good plugin .

    The logic works like, any change in the form it will check the form is valid or not. And also using the errorplacement function it will disable the default error message also.

    $().ready(function() {
        // validate signup form on keyup and submit
        $("#contactForm").validate({
            rules: {
                title: "required",
                description: {
                    required: true
                },
                newtag: {
                    required: true
                },
                category: {
                    required: true
                }
            },
            errorPlacement: function(error, element) {
                return true;
            },
            submitHandler: function() {
    
    
            }
        });
    
    
        $('#contactForm').change(function() {
            if ($("#contactForm").valid()) {
                $("#subnewtide").removeAttr("disabled");
            }
        });
    
    });
    

    Fiddle

提交回复
热议问题