Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent submitting blank input and textarea

I am working with an input a textarea and a input button . I need that the button is disabled when the input and textarea are empty.

This is Html

<input type="text" class="area">
<textarea name="testo" class="area" cols="30" rows="10"></textarea>
<input type="button" class="btn" value="send" disabled>

Javascript

$('.area').on('keyup' , function() {
    if( $('input').val().length > 0 && $('textarea').val().length > 0 ){
        $('.btn').prop('disabled', false);
    }
    else {
        $('.btn').prop('disabled', true);
    }
});

It works fine, but , if a press the spacebar and so it types blank chars , the lengths are not < 0 anymore and so the button not anymore disabled. How can I prevent this situation?

like image 738
steo Avatar asked Dec 28 '25 03:12

steo


1 Answers

Use $.trim() to clean up the values off of leading and trailing spaces

if( $.trim($('input').val()).length > 0 && $.trim($('textarea').val()).length > 0 ) {

}
like image 158
techfoobar Avatar answered Dec 30 '25 16:12

techfoobar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!