Skip to content Skip to sidebar Skip to footer

Stop Submit Button Being Pressed Until Enough Words Are Written In Text Area

So basically what I want is for the submit button to be disabled until a certain word count has been reached in the text area. I have had a look around and tried to find ways to do

Solution 1:

Show us your code.
1) Set onclick event for the submit button
2) Check length in textarea
3) return false and preventDefault() if there is not enough words.


Solution 2:

You can check the word/charactor count in keyup event.


Solution 3:

see this example at http://elylucas.net/post/Enabling-a-submit-button-when-a-textbox-has-value-in-jQuery.aspx

instead of checking for a value, you split the input string in the text box and see the length of the array is more than your desired word count


Solution 4:

Use the textchange (not built-in) event to accurately detect text changes via keyboard, paste etc. - http://www.zurb.com/playground/jquery-text-change-custom-event

$('textarea').bind('textchange', function () {
   // $(this).val() contains the new text
});

In your text-change event, check the length/count the words in your text and enable disable the submit button as needed (make sure its initially disabled).


Solution 5:

You catch the submit event, you count the words in the textarea and only submit if it is high enough. Example:

$('#targetForm').submit(function(event) {
    var text = $("#myTextarea").val();
    text = text.split(" ");
    // check for at least 10 words
    if(text.length < 10){
        // prevent submit
        event.preventDefault();
        return false;
    }
});

DEMO


Post a Comment for "Stop Submit Button Being Pressed Until Enough Words Are Written In Text Area"