user1547007
user1547007

Reputation: 267

How to disabled a submit button if textarea is empty on submit with jquery

There should be an easy way of validating this textarea without the use of a plug-in, been trying to check if textarea is empty or has an empty value but I just could not make this work. I have a http://jsfiddle.net/creativestudio/f3qQ5/

Hope someone could help.

This is my html:

<div class="post-container">
    <form class="reply-form">
        <div class="reply-box">
            <textarea placeholder="Reply box 1..." columns="10" rows="1" name="comment-input"></textarea>
            <input type="submit" value="Send">
        </div>
        <div class="post-dropdown"></div>
        <div class="post-dropdown-content">
            <div class="post-dropdown-reply">1</div>
        </div>
    </form>
</div>

<div class="post-container">
    <form class="reply-form">
        <div class="reply-box">
            <textarea placeholder="Reply box 2..." columns="10" rows="1" name="comment-input"></textarea>
            <input type="submit" value="Send">
        </div>
        <div class="post-dropdown"></div>
        <div class="post-dropdown-content">
            <div class="post-dropdown-reply hidden">1</div>
        </div>
    </form>
</div>

        ​

This is my Js:

function gettingReplyVal() {
    $('.reply-form').submit(function(e) {
        var textAreaValue = $(this).find('textarea').val();
        post = $("<div>").addClass("post-dropdown-reply");
        post.html(textAreaValue);
        $(this).find('.post-dropdown-content').prepend(post);
        e.preventDefault();
        $('textarea').val('');
    });
}

gettingReplyVal();​

Upvotes: 0

Views: 3282

Answers (4)

user1972007
user1972007

Reputation: 323

Try Following jquery script:

if($('textarea[name=comment-input]').val().trim()==''){
   $("#submit").attr("disabled", true);  
}

$('textarea[name=comment-input]').keyup(function(){
    $("#submit").attr("disabled", true);
    val = $(this).val().trim();    
    if(val.length > 0){
        $("#submit").attr("disabled", false);
    }
});

its work for me. I wish its also work for you.

Upvotes: 0

SubRed
SubRed

Reputation: 3187

You can use event keyup() if you want disable the submit button on empty:

$('textarea[name=comment-input]').keyup(function(e){
    $(this).parent().find("#submit").attr("disabled", true);
    val = $(this).val().trim();    
    if(val.length > 0){
        $(this).parent().find("#submit").attr("disabled", false);
    }
});

Look demo here http://jsfiddle.net/aanred/NpvsF/.

Upvotes: 1

Patrick Guimalan
Patrick Guimalan

Reputation: 1010

try this validation

if($(this).find('textarea').val()=='')
  return false;

Upvotes: 0

kennypu
kennypu

Reputation: 6051

$('.reply-form').submit(function(e) {
  if($(this).find('textarea').val().trim() == '')
    return false;
  //submit code here
}

You can just use .val(), but I added the trim() just to compensate for spaces.

Upvotes: 0

Related Questions