Stop submit button being pressed until enough words are written in text area

后端 未结 6 2320
感动是毒
感动是毒 2021-01-25 17:24

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

6条回答
  •  长发绾君心
    2021-01-25 18:00

    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

提交回复
热议问题