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>
5
  • 1
    use $("#sub").prop("disabled",true) Commented May 9, 2017 at 5:44
  • add type="submit" to your button. Read more from this stackoverflow.com/questions/30720354/… Commented May 9, 2017 at 5:46
  • nope.. Still not working man @CarstenLøvboAndersen Commented May 9, 2017 at 5:47
  • try either check if your have any errors in your console or add a console.log("trigger") inside your submitHandler. Commented May 9, 2017 at 5:49
  • @CarstenLøvboAndersen the problem is its not going into the submithandler Commented May 9, 2017 at 6:13

1 Answer 1

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

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

1 Comment

and please follow proper way to validate email like email:{required:true,email:true}

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.