Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable button until required fields are valid

I've searched for the answer to this problem but still can't get this to work:

<script type="text/javascript"> 
$(document).ready(function (){
    validate();
    $('#contact_subject').change(validate);
});

function validate(){
    if ($('#contact_subject').val().length   >   0  {
        $('button').prop('disabled', false);
    }
    else {
       $('button').prop('disabled', true);
    }
}

</script>

I want to make sure #contact_subject has data or else disable the button.

like image 493
Uriahs Victor Avatar asked Mar 16 '23 15:03

Uriahs Victor


2 Answers

I think you are missing a bracket ) in if condition

function validate(){
    if ($('#contact_subject').val().length   >   0)  {
        $('button').prop('disabled', false);
    }
    else {
       $('button').prop('disabled', true);
    }
}
like image 62
Nikhil Aggarwal Avatar answered Mar 18 '23 05:03

Nikhil Aggarwal


Change the event.Try this: http://jsfiddle.net/e0t7qv56/1/

$("#field").keyup(function(){
    console.log($(this).val().length);
    if($(this).val().length)
        $("#btn").prop('disabled', false);
    else
        $("#btn").prop('disabled', true);
});
like image 45
MikeVelazco Avatar answered Mar 18 '23 05:03

MikeVelazco