0

I want my Submit button disable until the elements in the form are not filled..!

I am using Laravel 5.4

There are only 3 fields out of which one is textbox and another is email and another is a select with options. Here is my Form..!

{{ Form::open(array('url' => 'student', 'id' => 'frmReg')) }}

<div class="form-group">
    <label>Name:</label>
    <input type="text" name="name" id="name" class="form-control">
    <div id="aName" style="color:red"></div>
</div>

<div class="form-group">
    <label>Email:</label>
    <input type="email" name="email" id="email" class="form-control">
    <div id="aEmail" style="color:red"></div>
</div>

<div class="form-group">
    <label id="gen">Gender:</label>
    <br>
    <select name="gender">
        <option value="Male">Male</option>
        <option value="Female">Female</option>
    </select>

</div>
<button class="btn btn-primary" id="sub">Create a student</button>
{{ Form::close() }}

Here is my jQuery script

<script>
var isValid=0;
    $(document).ready(function(){
       $("#frmReg").validate({
            rules: {
                name: {
                    required: true
                },
                email: {
                    required: true
                }
            },
            messages: {
                name: "Name is Empty",
                email: "Enter a valid Email ID..!"
            },
            submitHandler: function(form) { // <- pass 'form' argument in
                $("#sub").attr("disabled", true);
                form.submit(); // <- use 'form' argument here.
            }
        });
});
</script>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Jay Momaya
  • 1,831
  • 19
  • 32

1 Answers1

1

Just look at this code and place it where you are submiting your form dont use document.ready function.

$(document).on('click','#youBtnIdHere',function(){
 $("#frmReg").validate({
            rules: {
                name: {
                    required: true
                },
                email: {
                    required: true
                }
            },
            messages: {
                name: "Name is Empty",
                email: "Enter a valid Email ID..!"
            },
            submitHandler: `enter code here`function() { 
                $("#sub").attr("disabled", true);
                 $("#frmReg").submit(); 
            }
        }); });`

OR you can also set in your blad code like this way

    {{ Form::open(array('url' => 'student', 'i`enter code here`d' => 'frmReg','onsubmit' => '$("#yourBtnId").prop("disabled",true)')) }}

Hope this will help you

Shakti Sisodiya
  • 218
  • 2
  • 5
  • 14