How to check variable only with spaces in javascript?

1

I have a chat system, and the send button should not be enabled while the user does not type a letter.

In the example below the button is enabled when the user types something, but if he type only spaces the button is displayed.

$('#msg').on('keyup',function() {
var textarea_value = $("#msg").val();

if(textarea_value != '') {
    $('.button-enviar').attr('disabled' , false);
    $('.button-enviar').removeClass('disabled');
}else{
    $('.button-enviar').attr('disabled' , true);
    $('.button-enviar').addClass('disabled');
}
});

How to check if the variable textarea_value contains only spaces? So do not enable the button.

    
asked by anonymous 24.06.2016 / 13:55

1 answer

1

Taking advantage of the code you have and using trim () , it is used to remove the end / start spaces from a string:

$('#msg').on('keyup',function() {
    var textarea_value = $("#msg").val().trim();
    if(textarea_value != '') {
        $('.button-enviar').attr('disabled' , false);
        $('.button-enviar').removeClass('disabled');
    }
    else{
        $('.button-enviar').attr('disabled' , true);
        $('.button-enviar').addClass('disabled');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><textareaid="msg"></textarea>
<button type="button" class="button-enviar" disabled>ENVIAR</button>
    
24.06.2016 / 14:00