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
You can check the word/charactor count in keyup
event.
Try out below lines of code,
$(document).ready(function() {
$(".submit-name").attr("disabled", "true");
var minLength = 100;
$("#your-text-area").bind('keyup', function(event) {
var String = $("#your-text-area").val()
if (String.length >= minLength ) {
$(".submit-name").removeAttr("disabled");
} else {
$(".submit-name").attr("disabled", "true");
}
});
});
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.
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
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
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).